15

I'm looking to use regex to try remove all non alpha-numeric characters from a string and replace spaces with a +

All I want to permit is basically alphabetical words A-Z and +

This is specifically to prepare the string to become a part of a URL, thus need the + symbols instead of spaces.

I have looked at /\W+/ however this removes all white spaces and alpha-numeric characters, whereas I want to leave the spaces in if possible to then be replaced by + symbols.

I've searched around a bit but I can't seem to find something, I was hoping someone might have any simple suggestions for this.

Sample string & Desired result: Durarara!!x2 Ten -> durarara+x2+ten

2 Answers 2

47

This is actually fairly straightforward.

Assuming str is the string you're cleaning up:

str = str.replace(/[^a-z0-9+]+/gi, '+');

The ^ means "anything not in this list of characters". The + after the [...] group means "one or more". /gi means "replace all of these that you find, without regard to case".

So any stretch of characters that are not letters, numbers, or '+' will be converted into a single '+'.

To remove parenthesized substrings (as requested in the comments), do this replacement first:

str = str.replace(/\(.+?\)/g, '');

function replacer() {

  var str = document.getElementById('before').value.
    replace(/\(.+?\)/g, '').
    replace(/[^a-z0-9+]+/gi, '+');

  document.getElementById('after').value = str;
}

document.getElementById('replacem').onclick = replacer;
<p>Before:
  <input id="before" value="Durarara!!x2 Ten" />
</p>

<p>
  <input type="button" value="replace" id="replacem" />
</p>

<p>After:
  <input id="after" value="" readonly />
</p>

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

4 Comments

Thanks! This is almost there, just one question, this replaces parenthesis with a plus symbol also, such as: Arslan Senki (TV) into Arslan+Senki++TV+, is there a way it can not add the + but instead remove "(TV)" altogether? I've never been that great with regex, I appreciate your help!
Before the above replacement, add another line: str = str.replace(/\(.+?\)/g, '');
This is the best answer, I modified the second regex to be /\.|\(.+?\)/g to remove the periods/full-stops as well, should have mentioned it, my bad! Thanks a ton.
I needed to add it to a regexp like so const myRex = new RegExp('[^a-z0-9+]+,'g') then do str.replaceAll(myRex,'')
2
 str = str.replace(/\s+/g, '+');
 str  = str.replace(/[^a-zA-Z0-9+]/g, "");
  • First line replaces all the spaces with + symbol
  • Second line removes all the non-alphanumeric and non '+' symbols.

3 Comments

That would remove all non-alphanumeric chars in the first line. Nothing left to replace with + in the second line.
It'd be Super Awesome if you could describe your approach in addition to the code you post. :)
OP wants to replace every occurrence of space or non-alphanumeric symbol with JUST ONE '+' sign....durarara+x2+ten is desired result....

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.