简体   繁体   English

删除字符串中不是字母或数字的所有字符

[英]Remove all characters that are not letters or numbers in a String

how can i remove all characters that are not letters or 'numbers'?? 我怎样才能删除所有不是字母或'数字'的字符?

I have a string: 我有一个字符串:

var string = 'This - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';

And i want transform into this: 我想转变成这个:

var string = 'This is my 4 String and i want remove all characters 49494 that are not letters or numbers'

This is possible? 这个有可能?

Thanks!! 谢谢!!

You can use a regex like this: 你可以使用这样的正则表达式:

[\W_]+

The idea is to match with \\W a non word character (those characters that are not AZ , az , 0-9 and _ ) and also add explicitly _ (since underscore is considered a word character) 我们的想法是匹配\\W一个非单词字符(那些不是AZaz0-9_字符)并且还明确地添加_ (因为下划线被认为是单词字符)

Working demo 工作演示

var str = 's - is my 4 String, and i - want remove all characters 49494 that are not letters or "numbers"';     
var result = str.replace(/[\W_]+/g, ' ');

The way I like to do it is using a RegEx. 我喜欢这样做的方法是使用RegEx。 This will select all non -letters and non -numbers and replace them with nothing or deleting them: 这将选择所有计数器和计数器,并将其替换为空或删除它们:

string = string.replace(/[^\s\dA-Z]/gi, '').replace(/ +/g, ' ');

Explanantion: Explanantion:

[^  NOT any of there
  \s  space
  \d  digit
  A-Z letter
]

是的,可以使用正则表达式

string = string.replace(/[^a-z0-9]+|\s+/gmi, " ");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript删除字符串中不是数字,字母和空格的所有字符 - Javascript remove all characters from string which are not numbers, letters and whitespace 使用javascript,如何使字符串只有字母、数字和句点。 删除所有其他字符 - with javascript, how to make string only have letters, numbers, and period. Remove all other characters 正则表达式删除除数字以外的所有特殊字符? - Regex remove all special characters except numbers? jQuery删除所有字符,但数字和小数 - jQuery remove all characters but numbers and decimals 正则表达式JavaScript-如何检查字符串是否全是字母,然后是所有数字 - Regex JavaScript - how to check if a string is all letters and then all numbers 如何在字符串的任何第一个字母之前删除所有字符(在我的情况下为数字和点)? - How to remove all characters (in my case numbers and dots) before any first letter of a string? 删除除下划线、破折号和数字以外的所有字符和特殊字符 - Remove all the characters and special characters except underscores, dashes and numbers 用数字替换字符串中的字母 - Replace letters in string with numbers 如何从具有特殊字符的字符串中过滤所有小写字母? - How to filter all lowercase letters from a string having special characters? 正则表达式从javascript中的字符串中获取前3个字母和所有数字 - regex to get first 3 letters and all numbers from a string in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM