JS split() Regex with Three Matches to a single Results Array

javascript regex match example
javascript split regex
javascript regex match group
javascript regex multiple matches
javascript substring
javascript replace
javascript regex test
javascript split string at index

I'm trying to split a string of the type below in JS with split().

let shape = "Cube - Level 2: three-dimensional";

My desired end-state is something like:

0: "Cube"
1: "Level 2"
2: "three-dimensional"

I can individually capture words preceding the \s-\sLevel pattern with positive lookahead:


and the Level\s[0-9] pattern with a simple capture group:


as well as the arbitrary characters after Level:\s:


but I'm trying to figure out how to capture all three with JS split(). Is there a way to capture them such that split[0] = "Cube", split[1] = "Level 2", etc?

If you only want a solution that uses String.split() then my answer is wrong, but if you're looking for a general answer to the question of how to match the string, then I propose:

/^(.*?) - (Level (?:.*?)): (.*)$/.exec(str).slice(1)

So, for example:

let str = "Tetra - hedron - whatever - you - like - Level 867-5309: three-dimensional"

console.log(/^(.*?) - (Level (?:.*?)): (.*)$/.exec(str).slice(1))

It has 3 modes: let str = "I love JavaScript" ; let result = str . match ( /Java(Script)/ ) ; alert If the regexp has flag g , then it returns an array of all matches as Splits the string using the regexp (or a substring) as a delimiter. This is a generic method for searching and replacing, one of most useful ones. If the regular expression can match the empty string, Split will split the string into an array of single-character strings because the empty string delimiter can be found at every location. The following example splits the string "characters" into as many elements as the input string contains, starting with the character "a". Because the null

You could alternate between a dash (surrounded by spaces) and a colon followed by a space:

let shape = "Cube - Level 2: three-dimensional";
  shape.split(/ - |: /)

The split() method divides a String into an ordered set of substrings, console.​log(words[3]); separator can be a simple string or it can be a regular expression. the returned array contains one element consisting of the entire string. capturing parentheses, then each time separator matches, the results  Another simple but effective method is to use split + join repeatedly. "a=b,c:d".split('=').join(',').split(':').join(',').split(',') Essentially doing a split followed by a join is like a global replace so this replaces each separator with a comma then once all are replaced it does a final split on comma.

Create a character class with the characters , :, - and then split on 2 or 3 of those characters in row:

let shape = "Cube - Level 2: three-dimensional";
console.log(shape.split(/[ \-:]{2,3}/));

Thus, if match() returns an array a, a[0] contains the complete match, a[1] contains the "http" var host = result[2]; // Contains "www.example.com" var path = result[3]; The last of the regular-expression methods of the String object is split(). The RegExp() constructor takes one or two string arguments and creates a new  Create array of regex matches. Ask Question Asked 9 years ago. Active 1 year ago. Viewed 168k times 157. 47. In Java, I am trying to return all regex matches to an array but it seems that you can only check whether the pattern matches something or not (b

String.prototype.split() is a method that exists on all string objects and primitives and it's used to dice up a string into an array of strings at a given split market. The return result is always an array. The. Code. Listing 3-23. regular expressions (discussed at length in Chapter 20) to search for matches inside of the parent  To explain why your pattern didn't work, /-./ tells the regular expression engine to match a literal dash character followed by any character (dots are wildcard characters in regular expressions). With "02-25-2010", it would split each time "-2" is encountered, because the dash matches and the dot matches "2".

The [@@split]() method splits a String object into an array of strings by separating 3. const result = RegExp.prototype[Symbol.split].call(this, str, limit);. 4 match of this RegExp pattern, until the number of split items match the limit or This method is called internally in String.prototype.split() if its separator  Note that if you know the number of capturing groups you need per match, you can use this simple approach, which I present as an example (of 2 capturing groups.). Suppose you have some 'data' like

While simple patterns typically work the same way, patterns where no match is ECMA-262 indicates that the groups should be spliced into the result array. with split() and capturing groups in regular expressions, please see “JavaScript split returns one of three values as follows: If the string should come alphabetically  A single regex initialization isn't going to cost you much. If your objective is really to match the words in the array, then just go with String.indexOf, which is a non-regex form of solving the same.