Home > Article > Web Front-end > Introduction to different JavaScript string splitting methods
There are many ways to split and process js strings. Some common methods will be introduced below and specific code examples will be provided.
Code example:
let str = "hello world"; let arr = str.split(" "); console.log(arr); // 输出 ["hello", "world"]
Code example:
let str = "hello123world"; let arr = str.split(/d+/); console.log(arr); // 输出 ["hello", "world"]
Code example:
let str = "helloworld"; let arr = []; let length = 3; for(let i = 0; i < str.length; i += length) { arr.push(str.slice(i, i+length)); } console.log(arr); // 输出 ["hel", "low", "orl", "d"]
Code example:
let str = "helloworld"; let arr = []; let length = 3; for(let i = 0; i < str.length; i += length) { arr.push(str.substring(i, i+length)); } console.log(arr); // 输出 ["hel", "low", "orl", "d"]
Code examples:
let str = "hello world"; let arr = str.match(/w+/g); console.log(arr); // 输出 ["hello", "world"]
The above are some common methods and code examples for splitting and processing js strings. You can choose the appropriate method to process strings according to actual needs.
The above is the detailed content of Introduction to different JavaScript string splitting methods. For more information, please follow other related articles on the PHP Chinese website!