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

Community
  • 1
  • 1
Andi Giga
  • 3,744
  • 9
  • 38
  • 68

3 Answers3

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.

SGD
  • 1,676
  • 14
  • 18
  • 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. – Andi Giga Mar 25 '15 at 14:32
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/))
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
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.

Community
  • 1
  • 1
Andi Giga
  • 3,744
  • 9
  • 38
  • 68