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

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
Mathieu
  • 4,587
  • 11
  • 57
  • 112
  • Possible duplicate of https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript – Kévin Bibollet Mar 11 '19 at 09: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) – Mathieu Mar 11 '19 at 09: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 ( – Mathieu Mar 11 '19 at 09:44
  • I had a typo but posted a working answer. – anubhava Mar 11 '19 at 09:46

5 Answers5

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

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • @Mathieu Then you should accept answer (green check mark on the left) and optionally upvote. – Michał Turczyn Mar 11 '19 at 09:46
  • sure i'll do but i tried and SO says i have to wait minimum 5 minutes:) – Mathieu Mar 11 '19 at 09:47
  • i'll do it in 5 mins no worries – Mathieu Mar 11 '19 at 09:47
  • 1
    It seems OP wants to match many more special characters as attempted regex was: `text.replace(/\,$|:$|;$|-$|\($|–$|#$|\.$/, '');` – anubhava Mar 11 '19 at 09:48
  • 1
    @anubhava Sure. That's why I pointed "where you should list all unwanted characters" :) – Michał Turczyn Mar 11 '19 at 09:49
  • 1
    @MichałTurczyn i do get an error though "Invalid regular expression: /(\s*[,:;-\(])+$/: Range out of order in character class at :1:6" when one of the unwanted character i put is (. for ex: text.replace(/(\s*[,:;-\(])+$/, '') – Mathieu Mar 11 '19 at 09:59
  • I tried to escape it like so: text.replace(/(\s*[,:;-\(])+$/, '') but still same error – Mathieu Mar 11 '19 at 09:59
  • @Mathieu Try escaping hyphen, since it has special meaning inside square brackets: `\-` – Michał Turczyn Mar 11 '19 at 10:02
  • @Mathieu: this removes the last space, your question stated you want to keep it. – Toto Mar 11 '19 at 10:26
  • @Toto not exactly...i was ok with keeping a white space at the end IF the character before this white space is NOT one of the unwanted characters. sorry if I miscommunicated. – Mathieu Mar 11 '19 at 10:29
1

use this regex and try:

/(\s*[-,:;\(#\.])*$/
RK_15
  • 929
  • 5
  • 11
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);
anubhava
  • 761,203
  • 64
  • 569
  • 643
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*[-,:;(#.–])*$/, '');
}));
Toto
  • 89,455
  • 62
  • 89
  • 125
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 (:- '));
jo_va
  • 13,504
  • 3
  • 23
  • 47