0

I am trying to use JavaScript's string replace method to remove any characters or words from user input that do not match a set of words or codes within a custom pattern. For example, if I have I have a pattern %PT!!@@ and the input %PT1234!!test@@, I want to remove anything that doesn't match '%PT', '!!' and '@@'. This means '1234' and 'test' will be removed from the string. I have seen similar questions using the regular expression like (?!(%PT|!!|@@)) but this doesn't work.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
dan
  • 29
  • 6
  • What are you trying to accomplish in plain words not regex? Since your regex you know is buggy, plain words will help to debug? – GetSet Apr 03 '20 at 10:59
  • I want to remove any words or characters that don't match '%PT', '!!' or '@@'. If I do '%PT1234!!test@@''.replace(regex,''), I would have %PT!!@@ as the result. – dan Apr 03 '20 at 11:04
  • Can you give a real world example? Otherwise removing makes no sense if it has a definite value you want. – GetSet Apr 03 '20 at 11:10
  • How do you know that pattern should be `(%PT|!!|@@)` and not `(%PT!|!@@)` or `(%P|T!!@|@)`? – user2316116 Apr 03 '20 at 13:59
  • the pattern can be dynamic as it is defined by a user. I just used %PT!!@@ as an example – dan Apr 03 '20 at 16:01
  • If [my solution](https://stackoverflow.com/a/61011045/3832970) worked for you please consider accepting the answer by clicking ✓ on the left (see [How to accept SO answers](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)) and upvoting if my answer proved helpful to you (see [How to upvote on Stack Overflow?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)). – Wiktor Stribiżew Apr 04 '20 at 09:34

2 Answers2

1

So, you have a %PT1234!!test@@ string and you need to remove any text that doesn't match %PT, !! and @@ substrings.

You may capture these strings (or patterns) that you want to keep and just match everything else. Then, replace with the backreference to Group 1 to restore that value in the result.

Or, you may simple match what you need to get, and then join the found values.

See the JS demo:

console.log(
  "%PT1234!!test@@".replace(/(%PT|!!|@@)|[\s\S]/g, '$1')
) // => %PT!!@@
console.log(
  "%PT1234!!test@@".match(/%PT|!!|@@/g).join("")
) // => %PT!!@@

See the regex demo online.

Details

  • (%PT|!!|@@) - Capturing group 1: any of the values listed: %PT, !! or @@
  • | - or
  • [\s\S] - any char.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • The .match() function is great! Don't know why I didn't think of doing it that way. I think I was concentrating too much on the regular expression itself and removing values I didn't want rather than utilise the methods on the regex to capture the characters that did match the regex. Thanks for this, really appreciate it! – dan Apr 03 '20 at 12:15
  • In #1 you used `[\s\S]` rather than `.`. Please explain why. – Cary Swoveland Apr 03 '20 at 16:37
  • 1
    @CarySwoveland As usual, [to match any chars including line break chars](https://stackoverflow.com/a/45981809/3832970). – Wiktor Stribiżew Apr 03 '20 at 20:33
0

For your use case, you can use this regular expression: /[^%PT!{2}@{2}]/g

Quick example: https://jsfiddle.net/mzsavre5/

GoranLegenda
  • 343
  • 1
  • 7