Split JavaScript string with .match (regex)

In the Mozilla Developer Network for the split() function:

The split () method returns a new array.

Upon detection, the delimiter is removed from the string and the substrings are returned to the array. If the delimiter is not found or is omitted, the array contains one element consisting of the entire string. If separator is an empty string, str is converted to an array of characters.

If the delimiter is a regular expression that contains a parenthesis, then each time the delimiter is matched, the results (including any undefined results) of the sliding parentheses are spliced ​​into the output array. However, not all browsers support this feature.

Take the following example:

 var string1 = 'one, two, three, four'; var splitString1 = string1.split(', '); console.log(splitString1); // Outputs ["one", "two", "three", "four"] 

This is a really clean approach. I tried the same with regex and a slightly different line:

 var string2 = 'one split two split three split four'; var splitString2 = string2.split(/\ split\ /); console.log(splitString2); // Outputs ["one", "two", "three", "four"] 

This works as well as the first example. In the following example, I changed the line again using 3 different delimiters:

 var string3 = 'one split two splat three splot four'; var splitString3 = string3.split(/\ split\ |\ splat\ |\ splot\ /); console.log(splitString3); // Outputs ["one", "two", "three", "four"] 

However, regex is getting relatively messy right now. I can group different delimiters, however the result will include these delimiters:

 var string4 = 'one split two splat three splot four'; var splitString4 = string4.split(/\ (split|splat|splot)\ /); console.log(splitString4); // Outputs ["one", "split", "two", "splat", "three", "splot", "four"] 

So, I tried to remove the spaces from the usual expression, leaving the group effortlessly:

 var string5 = 'one split two splat three splot four'; var splitString5 = string5.split(/(split|splat|splot)/); console.log(splitString5); 

Although, when I remove the parentheses in the regex, the delimiter disappears in the split line:

 var string6 = 'one split two splat three splot four'; var splitString6 = string6.split(/split|splat|splot/); console.log(splitString6); // Outputs ["one ", " two ", " three ", " four"] 

An alternative would be to use match() to filter the delimiters, except that I don't understand how reverse lookups work:

 var string7 = 'one split two split three split four'; var splitString7 = string7.match(/((?!split).)*/g); console.log(splitString7); // Outputs ["one ", "", "plit two ", "", "plit three ", "", "plit four", ""] 

It does not match the whole word for a start. And honestly, I don’t even know what is going on here.


How to split a string using regular expressions without having a separator in my result?

+5
source share
2 answers

Use a non-capturing group as a split regular expression. Using a non-converting group, separate matches will not be included in the resulting array.

 var string4 = 'one split two splat three splot four'; var splitString4 = string4.split(/\s+(?:split|splat|splot)\s+/); console.log(splitString4); 
 // Output => ["one", "two", "three", "four"] 
+7
source

If you want to use match , you can write it as

 'one split two split three split four'.match(/(\b(?!split\b)[^ $]+\b)/g) ["one", "two", "three", "four"] 

What is he doing?

  • \b Matches the word boundary

  • (?!split\b) Negative look ahead, check if the word split

  • [^ $]+ Matches any space or $ , end of line. This template will match the word, the appearance ensures that it does not match the split .

  • \b Matches the end of a word.

+1
source

All Articles