[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split [Back]  [Original]

String.prototype.split() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

String.prototype.split()

20157

split()

const str = "The quick brown fox jumps over the lazy dog.";

const words = str.split(" ");
console.log(words[3]);
// Expected output: "fox"

const chars = str.split("");
console.log(chars[8]);
// Expected output: "k"

const strCopy = str.split();
console.log(strCopy);
// Expected output: Array ["The quick brown fox jumps over the lazy dog."]

js
split(separator)
split(separator, limit)

separator

undefined Symbol.split separator undefined split() undefined [Symbol.split]()

limit

split separator limit

  • limit
  • limit 0 []

separator

separator separator separatorTSV myString.split("\t") separator separator separator str

separator ""str UTF-16

"".split("") separator

separator UTF-16 code unit Unicode code point u

js
"".split(/(?:)/); // [ "\ud83d", "\ude04", "\ud83d", "\ude04" ]
"".split(/(?:)/u); // [ "", "" ]

separator separator undefined Symbol.split

separator Symbol.split limit this split

separator

split()

split() [""]

js
const emptyString = "";

// 
console.log(emptyString.split("a"));
// [""]

// 
console.log(emptyString.split(emptyString));
// []

separator

js
function splitString(stringToSplit, separator) {
  const arrayOfStrings = stringToSplit.split(separator);

  console.log("", stringToSplit);
  console.log("", separator);
  console.log(
    "",
    arrayOfStrings.length,
    "",
    arrayOfStrings.join(" / "),
  );
}

const tempestString = "Oh brave new world that has such people in it.";
const monthString = "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec";

const space = " ";
const comma = ",";

splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);

"Oh brave new world that has such people in it."
" "
 10 Oh / brave / new / world / that / has / such / people / in / it. /

"Oh brave new world that has such people in it."
"undefined"
 1 Oh brave new world that has such people in it. /

"Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
","
 12 Jan / Feb / Mar / Apr / May / Jun / Jul / Aug / Sep / Oct / Nov / Dec /

split() 0 0 nameList split

js
const names = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";

console.log(names);

const re = /\s*(?:;|$)\s*/;
const nameList = names.split(re);

console.log(nameList);

Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand
[ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", "" ]

split 0 3

js
const myString = "Hello World. How are you doing?";
const splits = myString.split(" ", 3);

console.log(splits);

["Hello", "World.", "How"]

RegExp

separator ( )

js
const myString = "Hello 1 word. Sentence number 2.";
const splits = myString.split(/(\d)/);

console.log(splits);
// [ "Hello ", "1", " word. Sentence number ", "2", "." ]

\d 0 9

Symbol.split

js
const splitByNumber = {
  [Symbol.split](str) {
    let num = 1;
    let pos = 0;
    const result = [];
    while (pos < str.length) {
      const matchPos = str.indexOf(num, pos);
      if (matchPos === -1) {
        result.push(str.substring(pos));
        break;
      }
      result.push(str.substring(pos, matchPos));
      pos = matchPos + String(num).length;
      num++;
    }
    return result;
  },
};

const myString = "a1bc2c5d3e4f";
console.log(myString.split(splitByNumber)); // [ "a", "bc", "c5d", "e", "f" ]

js
const DELIMITER = ";";

// 
const splitCommands = {
  [Symbol.split](str, lim) {
    const results = [];
    const state = {
      on: false,
      brightness: {
        current: 2,
        min: 1,
        max: 3,
      },
    };
    let pos = 0;
    let matchPos = str.indexOf(DELIMITER, pos);

    while (matchPos !== -1) {
      const subString = str.slice(pos, matchPos).trim();

      switch (subString) {
        case "light on":
          //  `on`  true
          if (!state.on) {
            state.on = true;
            results.push(subString);
          }
          break;

        case "light off":
          //  `on`  false
          if (state.on) {
            state.on = false;
            results.push(subString);
          }
          break;

        case "brightness up":
          // 
          if (state.brightness.current < state.brightness.max) {
            state.brightness.current += 1;
            results.push(subString);
          }
          break;

        case "brightness down":
          // 
          if (state.brightness.current > state.brightness.min) {
            state.brightness.current -= 1;
            results.push(subString);
          }
          break;
      }

      if (results.length === lim) {
        break;
      }

      pos = matchPos + DELIMITER.length;
      matchPos = str.indexOf(DELIMITER, pos);
    }

    //  `lim`
    if (results.length < lim) {
      results.push(str.slice(pos).trim());
    }
    return results;
  },
};

const commands =
  "light on; brightness up; brightness up; brightness up; light on; brightness down; brightness down; light off";
console.log(commands.split(splitCommands, 3)); // ["light on", "brightness up", "brightness down"]

ECMAScript 2027 LanguageSpecification
# sec-string.prototype.split


Web Proxy Viewer  |  New URL  |  Original Page