10 examples of 'javascript split string by whitespace' in JavaScript

Every line of 'javascript split string by whitespace' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
157function splitString(string) {
158 return string.split(new RegExp(separators.join('|'), 'g'));
159}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

64splitString(str) {
65 try {
66 return str ? str.split(this.delimStr) : [];
67 }
68 catch (error) {
69 return str;
70 }
71}
17function splitStringByNewline(string) {
18 return string.split('\n').filter(function(element) {
19 element = element.trim();
20 return element.length > 0;
21 });
22}
29function splitOnNewline(s) {
30 return s.trim().split(/\n/)
31}
190function splitString(str, ch) {
191 str = String(str);
192
193 var segs = str.split(ch);
194 var len = segs.length;
195 var res = [];
196 var i = -1;
197
198 while (++i < len) {
199 var key = segs[i];
200 while (key[key.length - 1] === '\\') {
201 key = key.slice(0, -1) + ch + segs[++i];
202 }
203 res.push(key);
204 }
205 return res;
206}
144function splitString(value) {
145 return value.split(/\s+/g);
146}
61export function splitByLines(text) {
62 return Array.isArray(text) ? text : text.split(/\r\n|\r|\n/g);
63}
31function splitStringRespectingQuotes(string, delim) {
32
33 var tokens = [],
34 len = string.length,
35 i,
36 n = 0,
37 quote = false,
38 c;
39
40 if (len > 0) {
41
42 tokens[n] = string.charAt(0);
43 for (i = 1; i < len; i++) {
44 c = string.charAt(i);
45 if (c === '"') {
46 quote = !quote;
47 } else if (!quote && c === delim) {
48 n++;
49 tokens[n] = "";
50 } else {
51 tokens[n] += c;
52 }
53 }
54 }
55 return tokens;
56}
12static split( s: string, regExp: RegExp ): string[] { return s.split( regExp ); }
168protected splitNewLine(str: string): string[] {
169 return str.split(this.newLineRegex);
170}

Related snippets