Javascript jQuery 从字符串中删除特殊字符等

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8979619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:17:03  来源:igfitidea点击:

jQuery remove special characters from string and more

javascriptjqueryregexreplace

提问by Roel

I have a string like this:

我有一个这样的字符串:

var str = "I'm a very^ we!rd* Str!ng.";

What I would like to do is removing all special characters from the above string and replace spaces and in case they are being typed, underscores, with a - character.

我想要做的是从上面的字符串中删除所有特殊字符并替换空格,如果它们被键入,下划线,带有 - 字符。

The above string would look like this after the "transformation":

上面的字符串在“转换”之后看起来像这样:

var str = 'im-a-very-werd-strng';

回答by Jasper

replace(/[^a-z0-9\s]/gi, '')will filter the string down to just alphanumeric values and replace(/[_\s]/g, '-')will replace underscores and spaces with hyphens:

replace(/[^a-z0-9\s]/gi, '')将字符串过滤为仅字母数字值,replace(/[_\s]/g, '-')并将用连字符替换下划线和空格:

str.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-')

Source for Regex: RegEx for Javascript to allow only alphanumeric

Regex 的来源:RegEx for Javascript to allow only alphanumeric

Here is a demo: http://jsfiddle.net/vNfrk/

这是一个演示:http: //jsfiddle.net/vNfrk/

回答by Ilia G

Assuming by "special" you mean non-word characters, then that is pretty easy.

假设“特殊”是指非单词字符,那么这很容易。

str = str.replace(/[_\W]+/g, "-")

回答by Grace Shao

str.toLowerCase().replace(/[\*\^\'\!]/g, '').split(' ').join('-')

回答by Pawan Singh Bisht

Remove numbers, underscore, white-spaces and special characters from the string sentence.

从字符串句子中删除数字、下划线、空格和特殊字符。

str.replace(/[0-9`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\/]/gi,'');

Demo

演示

回答by Amit Sharma

this will remove all the special character

这将删除所有特殊字符

 str.replace(/[_\W]+/g, "");

this is really helpful and solve my issue. Please run the below code and ensure it works

这真的很有帮助,可以解决我的问题。请运行以下代码并确保它有效

var str="hello world !#to&you%*()";
console.log(str.replace(/[_\W]+/g, ""));

回答by Marko Sulam?gi

Since I can't comment on Jasper's answer, I'd like to point out a small bug in his solution:

由于我无法评论 Jasper 的回答,我想指出他的解决方案中的一个小错误:

str.replace(/[^a-z0-9\s]/gi, '').replace(/[_\s]/g, '-');

The problem is that first code removes all the hyphens and then tries to replace them :) You should reverse the replace calls and also add hyphen to second replace regex. Like this:

问题是第一个代码删除了所有连字符,然后尝试替换它们:) 您应该反转替换调用并将连字符添加到第二个替换正则表达式。像这样:

str.replace(/[_\s]/g, '-').replace(/[^a-z0-9-\s]/gi, '');

回答by ghanshyam singh

Remove/Replace all special chars in Jquery :

删除/替换 Jquery 中的所有特殊字符:

If str = My name is "Ghanshyam" and from "java" background

如果str = 我的名字是“Ghanshyam”并且来自“java”背景

and want to remove all special chars (") then use it

并想删除所有特殊字符 (") 然后使用它

str=str.replace(/"/g,' ')

str=str.replace(/"/g,' ')

result: My name is Ghanshyam and from java background

结果:我的名字是 Ghanshyam,来自 Java 背景

Where g means Global @Thanks

其中 g 表示全局 @Thanks