我正在尝试验证字符串以使它们成为有效的网址
我只需要保留 A-Z 0-9 并使用以下命令从字符串中删除其他字符javascript or jquery
例如 :
贝儿餐厅
我需要将其转换为:
百丽餐厅
所以字符被删除,只保留 A-Z a-z 0-9
thanks
通过添加我们的.cleanup()
方法到 String 对象本身,然后您可以通过调用本地方法来清理 Javascript 中的任何字符串,如下所示:
# Attaching our method to the String Object
String.prototype.cleanup = function() {
return this.toLowerCase().replace(/[^a-zA-Z0-9]+/g, "-");
}
# Using our new .cleanup() method
var clean = "Hello World".cleanup(); // "hello-world"
因为正则表达式末尾有一个加号,所以它匹配一个or more人物。因此,输出总是有一个'-'
对于每个系列的一个或多个非字母数字字符:
# An example to demonstrate the effect of the plus sign in the regular expression above
var foo = " Hello World . . . ".cleanup(); // "-hello-world-"
如果没有加号,结果将是"--hello-world--------------"
对于最后一个例子。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)