将PHP正则表达式转换为Javascript
我有一个PHP正则表达式,用于从字符串中删除utf8特殊字符:将PHP正则表达式转换为Javascript,javascript,php,regex,Javascript,Php,Regex,我有一个PHP正则表达式,用于从字符串中删除utf8特殊字符: [\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF] 我需要将其转换为Javascript正则表达式。 我尝试了以下代码: str = str.replace(/[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[
[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF]
我需要将其转换为Javascript正则表达式。
我尝试了以下代码:
str = str.replace(/[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF]/g, '');
但它什么也没做
我需要你的帮助。
谢谢。试试这个:
str = str.replace(/[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF]/gi, '');
试试这个:
str = str.replace(/[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF]/gi, '');
试试这个:
str = str.replace(/[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF]/gi, '');
试试这个:
str = str.replace(/[\x00-\x1F]|\xC2[\x80-\x9F]|\xE2[\x80-\x8F]{2}|\xE2\x80[\xA4-\xA8]|\xE2\x81[\x9F-\xAF]/gi, '');
简单的错误,大的影响:
strTest = strTest.replace(/your regex here/g, "$1");
// ----------------------------------------^
如果没有“全局”标志,则仅对第一个匹配进行替换
旁注:要删除任何不满足某种复杂条件的字符,如落入一组特定Unicode字符范围,可以使用负前瞻:
var regex = /(?![\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})./g;
strTest = strTest.replace(regex, "")
其中regex
读作
(?! # negative look-ahead: a position *not followed by*:
[…] # any allowed character range from above
) # end lookahead
. # match this character (only if previous condition is met!)
(?!#消极前瞻:一个位置*后面没有*:
[…]#以上任何允许的字符范围
)#结束展望
. # 匹配此字符(仅当满足之前的条件时!)
简单的错误,大的影响:
strTest = strTest.replace(/your regex here/g, "$1");
// ----------------------------------------^
如果没有“全局”标志,则仅对第一个匹配进行替换
旁注:要删除任何不满足某种复杂条件的字符,如落入一组特定Unicode字符范围,可以使用负前瞻:
var regex = /(?![\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})./g;
strTest = strTest.replace(regex, "")
其中regex
读作
(?! # negative look-ahead: a position *not followed by*:
[…] # any allowed character range from above
) # end lookahead
. # match this character (only if previous condition is met!)
(?!#消极前瞻:一个位置*后面没有*:
[…]#以上任何允许的字符范围
)#结束展望
. # 匹配此字符(仅当满足之前的条件时!)
简单的错误,大的影响:
strTest = strTest.replace(/your regex here/g, "$1");
// ----------------------------------------^
如果没有“全局”标志,则仅对第一个匹配进行替换
旁注:要删除任何不满足某种复杂条件的字符,如落入一组特定Unicode字符范围,可以使用负前瞻:
var regex = /(?![\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})./g;
strTest = strTest.replace(regex, "")
其中regex
读作
(?! # negative look-ahead: a position *not followed by*:
[…] # any allowed character range from above
) # end lookahead
. # match this character (only if previous condition is met!)
(?!#消极前瞻:一个位置*后面没有*:
[…]#以上任何允许的字符范围
)#结束展望
. # 匹配此字符(仅当满足之前的条件时!)
简单的错误,大的影响:
strTest = strTest.replace(/your regex here/g, "$1");
// ----------------------------------------^
如果没有“全局”标志,则仅对第一个匹配进行替换
旁注:要删除任何不满足某种复杂条件的字符,如落入一组特定Unicode字符范围,可以使用负前瞻:
var regex = /(?![\x00-\x7F]|[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3})./g;
strTest = strTest.replace(regex, "")
其中regex
读作
(?! # negative look-ahead: a position *not followed by*:
[…] # any allowed character range from above
) # end lookahead
. # match this character (only if previous condition is met!)
(?!#消极前瞻:一个位置*后面没有*:
[…]#以上任何允许的字符范围
)#结束展望
. # 匹配此字符(仅当满足之前的条件时!)