How to split a string using split and RegExp?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
9 views
You need to split a string into 2 words using part of a word in the string.

var name = 'QWERTY'; var arr = name.split(new RegExp("QWE")) console.log(arr)


the console itself is truncated search word: ["a", "RTY"]

Thank you.
by | 9 views

2 Answers

0 like 0 dislike
So suitable:
var name = 'QWERTY'; var arr = name.replace("QWE",'QWE ').split(' ') console.log(arr)
by
0 like 0 dislike
To the consoleand not cut a word, it is necessary that the word came into result.
This is done in regexp with conventional braces:
var name = 'QWERTY'; var arr = name.split(new RegExp('(QWE)')); console.log(arr)


The only problem here: string 'QWE' splits the string 'QWERTY' into two parts, the first of which will still be an empty string.
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
asked Apr 9, 2019 by Zlatan77
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
35,458 users