Andi Giga
Andi Giga

Reputation: 4162

Javascript Regex Split After Certain Word or Number

The regex:

"12 DOse Flaschentomaten".split(/(\d+(?:\s(?:Dose|TL|EL)){0,})\s+(.*)/gi)

Gives me in the regex tool (https://www.regex101.com/) two groups:

12 DOse  
Flaschentomaten

Which is what I want.

But in the javascript console this array

["", "12 DOse", "Flaschentomaten", ""]

Side Questions: If I want to use an external array for the filter words do I have to define it as a string: var filterWords = "sp, ts, spoon"; Can I use the i tag for not case sensitive also only on groups?

Upvotes: 0

Views: 357

Answers (1)

georg
georg

Reputation: 214969

I guess you want match, not split:

m = "12 DOse Flaschentomaten".match(/(\d+(?:\s(?:Dose|TL|EL))?)\s+(.*)/i)
document.write("<pre>" + JSON.stringify(m,0,3));

Add m.shift() to remove the first element (=the whole match).

Upvotes: 1

Related Questions