0

Is there a way to use the replaceAll() function to remove these groups from the string?

Below is an example of the string:

0x0001,2: dummy text: dummy text 0x001B,0: more dummy text

I want to remove all the text that has this format 0x0000,0:

After the function is complete: it would look like this:

dummy text: dummy text more dummy text

  • 1
    have you tried anything? what does not work? – Nina Scholz Dec 17 '20 at 14:38
  • 4
    You can use a regular expression to match all sequences that match that pattern and replace them with an empty string. – Felix Kling Dec 17 '20 at 14:38
  • Does this answer your question? [How to replace all occurrences of a string?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string) – Dane Brouwer Dec 17 '20 at 14:47
  • You can always pass in a regex and replace matching strings with an empty string . I am assuming that you are trying to remove <4-digit hex >,<1 digit decimal number>: from your string . If its so you can try .replaceAll(/0x[0-9A-F]{1,4},[0-9]:/g,'') . please update the question with correct pattern you want to match – Alvin Zachariah Dec 17 '20 at 15:02

2 Answers2

1

You can replace by this regular expression: 0x[\dA-F]{4},\d\:

'0x0001,2: dummy text: dummy text 0x001B,0: more dummy text'.replace(/0x[\dA-F]{4}\,\d\:/g,'')
0

There is a way actually using RegEx, a class in Node.js.

const replacer = new RegExp("0x\\w{4},\\d: ", 'g')
const inStr = '0x0001,2: dummy text: dummy text 0x001B,0: more dummy text'
const outStr = inStr.replace(replacer, '')
console.log("Input: ", inStr)
console.log("Output: ", outStr)

Here is a good link to get familiar with regular expressions. https://www.rexegg.com/regex-quickstart.html

The method in the answer above prints an additional space where the expression is removed. However, this is a relatively easy fix.