Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
480 views
in Technique[技术] by (71.8m points)

javascript - 删除除第一个+运算符外的所有非数字字符(Remove all non numeric characters except first + operator)

I want to remove all non-numeric characters except the first + operator.(我想删除除第一个+运算符外的所有非数字字符。)

So + operator should show at the first.(因此,+运算符应首先显示。)

For example,(例如,)

+614a24569953 => +61424569953(+ 614a24569953 => +61424569953)

+61424569953+ => +61424569953(+61424569953+ => +61424569953)

  ask by yu-coder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Maybe,(也许,)

(?!^+)[^d
]+

replaced with an empty string would simply do that.(替换为空字符串就可以做到这一点。)


The first statement,(第一个陈述,)

(?!^+)

ignores the + at the beginning of the string, and the second one,(忽略字符串开头的+ ,第二个忽略,)

[^d
]+

ignores digits, newlines and carriage returns in the string.(忽略字符串中的数字,换行符和回车符。)

RegEx Demo(正则演示)

Test(测试)

 const regex = /(?!^\+)[^\d\r\n]+/g; const str = `+614a24569953`; const subst = ``; const result = str.replace(regex, subst); console.log(result); 


If you wish to simplify/update/explore the expression, it's been explained on the top right panel of regex101.com .(如果您想简化/更新/探索该表达式,请在regex101.com的右上方面板中进行说明 。)

You can watch the matching steps or modify them in this debugger link , if you'd be interested.(如果您有兴趣,可以观看匹配的步骤或在此调试器链接中对其进行修改。) The debugger demonstrates that how a RegEx engine might step by step consume some sample input strings and would perform the matching process.(调试器演示了RegEx引擎如何逐步使用一些示例输入字符串并执行匹配过程。)

RegEx Circuit(RegEx电路)

jex.im visualizes regular expressions:(jex.im可视化正则表达式:)

在此处输入图片说明


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...