javascriptregexreplaceall

Remove parts of substring with couple of conditions


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


Solution

  • You can use

    const arr = ["GH82-30476E/30477E", "GH82-23124B GH82-23100B", "GH82-20900A, GH82-20838A", "GH82-20900C,GH82-20838C", "GH962-13566A", "GH82-23596C/23597C/31461C"]
    
    arr.forEach((e, index) => {
      arr[index] = e.replace(/GH\d*-/gi, '').split(/\W+/).join(" ");
    
    
    })
    console.log(arr)

    Here