Hans Hansen
Hans Hansen

Reputation: 49

regular expressions to remove semicolons in string

I'm trying to remove semicolons from the end of matches inside a string

so:

 var ourstring = " (1); (2);; (3) ; (4) ;; (5) ; ; (6); ;  (7); ;; (8)";


become:

(1) (2) (3) (4) (5) ; (6) ; (7) ;; (8)

The problem is i don't know how to do that. I have tried to use regular expressions: \s*(?=[;*])[;]+(?!\s)?/g inside replace, but did not get the wanted results. Please show me how to do that in plain javascript

Upvotes: 1

Views: 525

Answers (1)

user9366559
user9366559

Reputation:

I guess this is what you want.

var ourstring = " (1); (2);; (3) ; (4) ;; (5) ; ; (6); ;  (7); ;; (8)";
var re = /\)\s*;+/g;
var result = ourstring.replace(re, ")");

console.log(result);

It removes zero or more spaces followed by one or more adjacent semicolons that come directly after a closing parenthesis.

Upvotes: 5

Related Questions