how can I "clean" strings from everything that starts with GH
until -
mark, I need just the rest of the data, the problem is that I don’t know what number will be part of GHxxx-
,GHxx-
in the example, it can be anything.
Is there a way in regex to define something like this?
GH8-30476E/30477E
GH82-23124B GH82-23100B
GH82-20900Aaa, GH82-20838A
GH82-20900C,GH82-20838C
GH962-13566A
GH82-23596C/23597C/31461C
desired result:
30476E 30477E
23124B 23100B
20900Aaa 20838A
20900C 20838C
13566A
23596C 23597C 31461C
const arr = ["GH8-30476E/30477E", "GH82-23124B GH82-23100B", "GH82-20900Aaa, GH82-20838A", "GH82-20900C,GH82-20838C", "GH962-13566A", "GH82-23596C/23597C/31461C"]
arr.forEach((e, index) => {
arr[index] = e.replace(/GH82-/gi, '');
})
console.log(arr)
Thank you in advance.
edit:
GH82-23100B
can be GH82-2310044B
or GH82-23B
, that is why my approach was to remove GH, not match the other 5 characters.
edit2:
I edited the examples a bit. Looks like solution from comment works
3
Answers
s*[A-Z]{2}[0-9]{1,3}s*-s*|[/,.]
, instead:You can add more restrictions to the pattern, depending on how your data may look like:
[/,.]+
, anything that wanted to be removed, goes into this character class.b
, is a word boundary.GH
, then we simply use:/s*bGH[0-9]{1,3}bs*-s*|[/,.]+/g
There are two way you did that.
Method-01: find matched substring of format
d{5}[A-Z]
and then join them.Method-02: remove
GHd+-
substring and/
or,
special characters, then trim the data.Code as follows:
You can use
Here
.replace(/GHd*-/gi, '')
– removesGH
+ zero or more digits and then-
.split(/W+/)
– splits at non-word chars.join(" ")
– joins the resulting items with a space.