Reputation: 1213
So basically given this string iiisdoso
i want to extract all the part of the string before but excluding the letter o
.
So basically iiisd
and s
.
I current just have
const data = "iiisdoso"
const regex = /(.*?)o/g
console.log(data.match(regex))
but this result includes the o in but my goal is return results up to but excluding o
Upvotes: 0
Views: 38
Reputation: 447
As already mentioned in the comments you can achieve this with negations and character classes:
const data = "iiisdoso"
const regex = /([^o]+)/g
console.log(data.match(regex))
This will give you the desired result
["iiisd", "s"]
But this does match any string with no "o" after it too! To avoid this, you need to use
const regex = /([^o]+)o/g
In this case the "o" is included in the matches and must be deleted for each string the result-array - e.g. with
mymatch.replace('o','')
Interesting: If you use
const regex = /([^o]*)/g
The result is
["iiisd", "", "s", "", ""]
This is something I don't understand. Well - I understand that an empty string matches the regex too and can be placed right before the "o". But why do we get a third empty string at the very end?
Upvotes: 1