tousle
tousle

Reputation: 110

Javascript regex - remove all special characters except semi colon

In javascript, how do I remove all special characters from the string except the semi-colon?

sample string: ABC/D A.b.c.;Qwerty

should return: ABCDAbc;Qwerty

Upvotes: 7

Views: 17093

Answers (2)

jfriend00
jfriend00

Reputation: 708046

You can use a regex that removes anything that isn't an alpha character or a semicolon like this /[^A-Za-z;]/g.

const str = "ABC/D A.b.c.;Qwerty";
const result = str.replace(/[^A-Za-z;]/g, "");
console.log(result);

Upvotes: 16

gdoron
gdoron

Reputation: 150303

var str = "ABC/D A.b.c.;Qwerty";
var result = str.replace(/[^A-Za-z;]/g, "");​​ // 21ABCDAbc;Qwerty

Live DEMO

Upvotes: 7

Related Questions