英文:
combining multiple regex into single
问题
我有一个很长的字符串,我想用空字符替换其中的一些字符。
我有以下正则表达式:
-
首先,我想从字符串中删除所有的URL,对应的正则表达式是
/(?:https?|ftp):\/\/[\n\S]+/i
-
接下来,我想删除所有以Car开头后跟着九个数字的单词。Car不区分大小写,对应的正则表达式是
/car\s*\d{9}/i
-
最后,我想从字符串中删除所有的特殊字符,对应的正则表达式是
/[^\w\s]/gi
所以我的输入字符串如下:
输入
Hell Test https://regex101.com with special Car123456789dgd cha cAr12345678racters @##!$#!@Hekki
输出Hell Test with special dgd cha cAr12345678racters Hekki
目前我需要进行三个单独的操作来完成这个任务,有没有办法将这些正则表达式合并成一个呢?
我尝试使用|
来连接它们,但没有成功。
(/(?:https?|ftp):\/\/[\n\S]+/i)|(/car\s*\d{9}/i)|(/[^\w\s]/gi)
英文:
I have long string and I want to replace some chars with empty.
I have following regex
-
1st I want to remove all url from string, for which I have regex
/(?:https?|ftp):\/\/[\n\S]+/i
-
Next I want to remove all word which starts with Car followed by nine digits. Car word should case insensitive, regex for that
/car\s*\d{9}/i
-
And Last I want to remove all special chars from string, regex for that
/[^\w\s]/gi
So my input string like below
Input
Hell Test https://regex101.com with special Car123456789dgd cha cAr12345678racters @##!$#!@Hekki
OutputHell Test with special dgd cha cAr12345678racters Hekki
currently I have to do 3 separate operation to do this, Is there any way I can combine these regex into one?
I try to join using |
but didn't work.
(/(?:https?|ftp):\/\/[\n\S]+/i)|(/car\s*\d{9}/i)|(/[^\w\s]/gi)
答案1
得分: -1
使用|
来组合:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const str = 'Hell Test https://regex101.com with special Car123456789dgd cha cAr12345678racters @##!$#!@Hekki';
console.log(str.replace(/(?:https?|ftp):\/\/[\n\S]+|car\s*\d{9}|[^\w\s]/gi, ''));
<!-- end snippet -->
英文:
Use |
to combine:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const str = 'Hell Test https://regex101.com with special Car123456789dgd cha cAr12345678racters @##!$#!@Hekki';
console.log(str.replace(/(?:https?|ftp):\/\/[\n\S]+|car\s*\d{9}|[^\w\s]/gi, ''));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论