1

I want to remove certain characters when they are at the end of a string. I have a working regex but the issue is the following:

My code:

var text = "some string";
text.replace(/\,$|:$|;$|-$|\($|–$|#$|\.$/, '');

So let's see the issue

If text = "that a nice (", then my code outputs "that a nice ", which is exactly what I want.

But if if text has multiple matches such as

text = "that a nice ( #", then the output of my current code is: "that a nice ( "... but I want to remove also the ( when it's at the end of the string as you can see on my regexp. I'd need to trim it so that the white space is removed but then how to remove the ( again...

The question is how to remove any unwanted characters and make sure that the new output also does not include these characters at their (new) end ? I could have 2, 3 or whatever number of unwanted characters at the end

4
  • Possible duplicate of stackoverflow.com/questions/1144783/… Commented Mar 11, 2019 at 9:40
  • @iArcadia no my question is not about removing ALL occurences of a substring but remove them when they're at the END of a string (or at the end just before a white space at the end) Commented Mar 11, 2019 at 9:42
  • 1
    @anubhava no it does not work. "that a nice ( #"".replace(/[-,:;(#.–]+$/, '') is outputting "that a nice ( ", which is alreayd what i get but i don't want any trailing ( Commented Mar 11, 2019 at 9:44
  • I had a typo but posted a working answer. Commented Mar 11, 2019 at 9:46

5 Answers 5

1

You could use pattern: (\s*[#(])+$

\s*[#(] matches zero or more spaces, then one or more one of characters in square brackets, where you should list all unwanted characters. And then it matches this whole pattern on ro more times. $ matches end of string to assure that we are at the end of a string.

Demo

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

10 Comments

@Mathieu Then you should accept answer (green check mark on the left) and optionally upvote.
sure i'll do but i tried and SO says i have to wait minimum 5 minutes:)
It seems OP wants to match many more special characters as attempted regex was: text.replace(/\,$|:$|;$|-$|\($|–$|#$|\.$/, '');
@anubhava Sure. That's why I pointed "where you should list all unwanted characters" :)
@MichałTurczyn i do get an error though "Invalid regular expression: /(\s*[,:;-(])+$/: Range out of order in character class at <anonymous>:1:6" when one of the unwanted character i put is (. for ex: text.replace(/(\s*[,:;-(])+$/, '')
|
1

use this regex and try:

/(\s*[-,:;\(#\.])*$/

Comments

1

You may use a character class with + quantifier that should include \s (whitespace):

var text = "that a nice ( #";
text = text.replace(/[-,\s(){}:;(#.–]+$/, '');
console.log(text);

Comments

0

This will remove all unwanted character at the end and keep a space.

var test = [
    "that a nice (",
    "that a nice ( #",
];
console.log(test.map(function (a) {
  return a.replace(/[-,:;(#.–](?:\s*[-,:;(#.–])*$/, '');
}));

Comments

0

Use a character group and match it any times at the end /[(,:;\-–#\.\s]*$/:

const sanitize = text => text.replace(/[(,:;\-–#\.\s]*$/, '');

console.log(sanitize('this is a nice,:;-–#.'));
console.log(sanitize('this is a nice ( '));
console.log(sanitize('this is a nice ( #'));
console.log(sanitize('this is a nice (:- '));

Comments

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.