0

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?

4
  • What is the "main" question ? if you're asking why you get this result, it's because you are splitting your string I guess, regex101 only get the matches of your regex Commented Mar 17, 2015 at 9:03
  • what's the issue you've hit here? is the question how to get your array of results into a string? Commented Mar 17, 2015 at 9:04
  • The issue/main question is how to split phrases like 1 Dose Banana, 1 Banana or 1 TL Banana into two strings. The first should contain the 1 + an optional Word the second should contain the rest (in the example Banana). Unfortunately my regex seems to split the phrase into 4 pieces. Commented Mar 17, 2015 at 9:06
  • I could use array.shift() and array.pop() afterwards but why does it add the empty strings in the first place/ and how to avoid that? Commented Mar 17, 2015 at 9:14

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.