[英]Regex that match strings in between underscores, asterisks & tilde
The closes I got is this _(.*?)_
我结账的是这个
_(.*?)_
What regex should I use to get every instance of string in between underscores but should not be a substring of a string (check test3) and should not also get those in new line (check test4) 我应该使用什么正则表达式来获取下划线之间的字符串的每个实例,但不应该是字符串的子字符串(检查test3),也不应该将它们放在换行符中(检查test4)
var string = "_test1 test2_ test_3_ _\n test4_ _test5_"
The regex should only get _test1 test2_
& _test5_
正则表达式应该只获得
_test1 test2_
和_test5_
Note: any character/string the word test doesn't matter 注意:单词test的任何字符/字符串都没有关系
If you can guarantee that each underscore pair is separated by a whitespace character (so no two underscores are next to one another), then this regex will work: \\b_([\\w\\s]+?)_\\b
如果可以保证每个下划线对之间都由一个空格字符分隔(因此,两个下划线不会相邻),那么此正则表达式将起作用:
\\b_([\\w\\s]+?)_\\b
To match any of the 3 cases you need, use this regex: (?:\\b|\\s|^)[_*~]([\\w\\s]+?)[_*~](?:\\s|$)
要匹配您需要的3种情况,请使用以下正则表达式:
(?:\\b|\\s|^)[_*~]([\\w\\s]+?)[_*~](?:\\s|$)
You can use this regex 您可以使用此正则表达式
(?:^| )(_(?:[\w ]+?)_|([*~])(?:[\w ]+?)\2)(?= |$)
JS Demo JS演示
var re = /(?:^| )(_(?:[\\w ]+?)_|([*~])(?:[\\w ]+?)\\2)(?= |$)/g; var str = "_test1 test2_ test_3_ _\\n test4_ *test5* test*3* _asdf_ _ghjkl_ _mno_"; document.writeln("<pre>" + str.match(re) + "</br>" + "</pre>");
For getting the required value from 为了从中获得所需的值
test1 test2 test_3_ \\n test4 test5
test1 test2 test_3_ \\ n test4 test5
use 采用
_test[^_\n]+_
The below one works as well. 下面的一个也可以。
^_([^_]+?)_|_([^_]+?)_$
text.match(/^_([^_]+?)_|_([^_]+?)_$/g);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.