Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 555
tousle Avatar asked May 01 '12 22:05

tousle


2 Answers

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);
like image 166
jfriend00 Avatar answered Oct 11 '22 21:10

jfriend00


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

Live DEMO

like image 43
gdoron is supporting Monica Avatar answered Oct 11 '22 20:10

gdoron is supporting Monica