1

I want to remove all of the symbols (The symbol depends on what I select at the time) after each word, without knowing what the word could be. But leave them in before each word.

A couple of examples:

!!hello! my! !!name!!! is !!bob!! should return...

!!hello my !!name is !!bob ; for !

and

$remove$ the$ targetted$@ $$symbol$$@ only $after$ a $word$ should return...

$remove the targetted@ $$symbol@ only $after a $word ; for $

1
  • And you've tried...what? Commented Oct 11, 2016 at 23:32

1 Answer 1

1

You need to use capture groups and replace:

"!!hello! my! !!name!!! is !!bob!!".replace(/([a-zA-Z]+)(!+)/g, '$1');

Which works for your test string. To work for any generic character or group of characters:

var stripTrailing = trail => {
  let regex = new RegExp(`([a-zA-Z0-9]+)(${trail}+)`, 'g');
  return str => str.replace(regex, '$1');
};

Note that this fails on any characters that have meaning in a regular expression: []{}+*^$. etc. Escaping those programmatically is left as an exercise for the reader.

UPDATE

Per your comment I thought an explanation might help you, so:

First, there's no way in this case to replace only part of a match, you have to replace the entire match. So we need to find a pattern that matches, split it into the part we want to keep and the part we don't, and replace the whole match with the part of it we want to keep. So let's break up my regex above into multiple lines to see what's going on:

First we want to match any number of sequential alphanumeric characters, that would be the 'word' to strip the trailing symbol from:

(       // denotes capturing group for the 'word'
  [     // [] means 'match any character listed inside brackets'
    a-z // list of alpha character a-z
    A-Z // same as above but capitalized
    0-9 // list of digits 0 to 9
  ]+    // plus means one or more times
)

The capturing group means we want to have access to just that part of the match. Then we have another group

(
  ! // I used ES6's string interpolation to insert the arg here
  + // match that exclamation (or whatever) one or more times
)

Then we add the g flag so the replace will happen for every match in the target string, without the flag it returns after the first match. JavaScript provides a convenient shorthand for accessing the capturing groups in the form of automatically interpolated symbols, the '$1' above means 'insert contents of the first capture group here in this string'.

So, in the above, if you replaced '$1' with '$1$2' you'd see the same string you started with, if you did 'foo$2' you'd see foo in place of every word trailed by one or more !, etc.

Sign up to request clarification or add additional context in comments.

6 Comments

Perfect, thanks. Where did you learn your 'regex skills'? Want to improve mine.
Mostly here on SO, so you're on the right track. Learn Regex the Hard Way by Zed Shaw is a good resource. But the best thing to do head over to a site like regex101 and start playing, because it gives you an explanation of the regex. regex101.com/r/tvGQ64/1
@Kreitzo added explanation to the answer, hope that helps you on the path.
Couldn't positive lookaheads be used here too?
@GGG I don't think so, it would be a lookbehind to match the word after matching !+ and JS can't do lookbehinds. I played with it and didn't get it to work, feel free to post an alternate answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.