0

This splits the string as wanted but doesn't keep the seperator:

"200 g Joghurt".split(/[0-9]?\s?[–|-]?\s?[0-9]+\s?g{0,1}\b/)
["", " Joghurt"]

I have cases like this

  • 100-200g
  • 1g
  • 240 - 1500 g

This thread suggest to use a positive look ahead. Javascript and regex: split string and keep the separator

But if I put it in it splits to much:

"200 g Joghurt".split(/(?=[0-9]?\s?[–|-]?\s?[0-9]+\s?g{0,1}\b)/)
["2", "0", "0 g Joghurt"]

Question: How to split that string above (numbers can be any number, Joghurt can be any word) after the numbers and g. e.g.

["200-400g", "joghurt"]

EDIT: There are cases where I don't have the g e.g.

  • 1 song
  • song
  • michael jackson song

So just matching with the g does not work. The cases I put in the examples are the cases with an g. And the g is always combined with one number or a range of numbers. Sorry about the confusion. I want to split recipe ingredients into the ingredient and the amount. The g is gram, maybe that makes the use case more clear.

EDIT 2: I also modified it a bit, the regex basically works as you can see in this example: https://regex101.com/r/rL7hY9/1 But it does not split it in a whole block. I also have this problem with my other regex cases, the match on regex101 is the whole group. But .split() does not split it in one part (with the positive lookahead).

3
  • regex101.com/r/fB6jO3/2 ? Something like this? I've used capturing groups.... Commented Mar 24, 2015 at 16:42
  • 3
    wait, how does 200 g Joghurt becomes ["200-400g","joghurt"]? Commented Mar 24, 2015 at 16:43
  • Actually, what is desired output here at all? 1)amount/mass + 2)ingredient ? Commented Mar 24, 2015 at 16:52

3 Answers 3

0

If you always have the "g " you could use this and re-add the "g" to the first array element. That might be easier than trying to create some vast RegEx. If it needs to be RegEx only you probably want to match a space with a negative lookbehind for the "g". Lookbehinds are not available in all RegEx engines however.

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

1 Comment

That is exactly the problem. Sorry for not mentioning it. This is just 1 of maybe 50 regex cases I have to filter. There are cases with no g.
0

You can use this regex - (?!\s*g)\s. We match a space that is preceded with "g":

alert("200 g Joghurt".split(/(?!\s*g)\s/))

Comments

0

Ok I found the answer. The first stackoverflow link I posted suggsted the positive lookahead. But it is easier than that, you just need to place the regex expression in brackets:

"200 g Joghurt".split(/([0-9]+\s?[–|-]?\s?[0-9]*\s?g{0,1}\b)/gi) Javascript - string.split(regex) keep separators After that I remove empty strings and whitespaces.

Another workaround I created is. Use .match first, this delivers the matched regex and then use .split afterwards which cuts the regex-part away. Which is only easy if there is no global flag.

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.