如何删除特殊字符?

时间:2019-01-18 18:06:03

标签: javascript

用于删除特殊字符的脚本。

2 个答案:

答案 0 :(得分:1)

You can use alternation and grouping.

([a-z]+)\. - Matches one or more character (captures as group) followed by dot.

\.([a-z]+) - Matches dot followed by one or more character (captures chracters as group)

And in replace by the matched group.

let str = `ABCD, N.C. exg. 58/2095, s. 2.7 `

let op = str.replace(/([a-z]+)\.|\.([a-z]+)/ig, '$1')

console.log(op)

答案 1 :(得分:1)

If your input is only ASCII, just replace every letter followed by a dot with the character itself:

console.log(
  "ABCD, N.C. exg. 58/2095, s. 2.7".replace(/([a-z])\./ig, '$1') 
);

Removing every special character.

Because that's what you are telling it to do. [^\w.\s] matches every character that is not a letter, number, _, . or white space.