从这个q/a,我推断出匹配给定正则表达式的所有实例不在引号内,是不可能的.也就是说,它无法匹配转义引号(例如:)"this whole \"match\" should be taken".如果有一种我不知道的方法,这将解决我的问题.
但是,如果没有,我想知道是否有任何可用于JavaScript的有效替代方案.我已经考虑了一下,但是没有任何优雅的解决方案可以在大多数(如果不是全部)情况下使用.
具体来说,我只需要使用.split()和.replace()方法的替代方法,但如果它可以更通用化,那将是最好的.
例如:
输入字符串:
+bar+baz"not+or\"+or+\"this+"foo+bar+
replace + with#,而不是引号内,将返回:
#bar#baz"not+or\"+or+\"this+"foo#bar#
我有一个正则表达式
/^([a-zA-Z0-9]+)$/
这只允许使用字母数字,但如果我只插入数字或只插入字符,那么它也接受它.我希望它工作,因为字段应该只接受字母数字值,但该值必须至少包含1个字符和1个数字.
我有一个包含一定数量行的文件.每一行看起来像这样:
TF_list_to_test10004/Nus_k0.345_t0.1_e0.1.adj:PKMYT1
Run Code Online (Sandbox Code Playgroud)
我想删除所有":"之前的字符,以便仅保留作为基因名称的PKMYT1.由于我不是正则表达式脚本编写的专家,任何人都可以帮助我使用Unix(sed或awk)或R?
将它们放入正则表达式时,我是否必须逃避斜线?
myString = '/courses/test/user';
myString.replace(/\/courses\/([^\/]*)\/.*/, "$1");
document.write(myString);
Run Code Online (Sandbox Code Playgroud)
它不打印"test",而是打印整个源字符串.
看这个演示:
我有一个var,其中包含以这种格式列出的大量单词(数百万):
var words = "
car
house
home
computer
go
went
";
Run Code Online (Sandbox Code Playgroud)
我想创建一个函数,用空格替换每个单词之间的换行符.
所以结果看起来像这样:
car house home computer go went
Run Code Online (Sandbox Code Playgroud) 我在我的网页上使用javascript.我试图通过文本框搜索一个完整的单词.说我搜索:'我',我应该在文本中找到所有'我',但不会发现'memmm'.
我正在使用javascript的搜索('我的正则表达式')来执行当前搜索(没有成功).
谢谢!
编辑:在几个建议使用\ b开关[似乎不起作用]后,我发布了我的问题的修订说明:
amm,从某种原因来看,似乎没有做这些伎俩.假设以下javascript搜索文本:
<script type='text/javascript'>
var lookup = '\n\n\n\n\n\n2 PC Games \n\n\n\n';
lookup = lookup.trim() ;
alert(lookup );
var tttt = 'tttt';
alert((/\b(lookup)\b/g).test(2));
</script>
Run Code Online (Sandbox Code Playgroud)
移动线是必不可少的
我有一个混合二进制数据和文本数据的文件.我想通过正则表达式解析它,但我收到此错误:
TypeError: can't use a string pattern on a bytes-like object
Run Code Online (Sandbox Code Playgroud)
我猜这个消息意味着Python不想解析二进制文件.我正在打开带有"rb"标志的文件.
如何在Python中使用正则表达式解析二进制文件?
编辑:我正在使用Python 3.2.0
我有一个字符串,我想从中删除数字之间的空格:
string test = "Some Words 1 2 3 4";
string result = Regex.Replace(test, @"(\d)\s(\d)", @"$1$2");
Run Code Online (Sandbox Code Playgroud)
预期/期望的结果将是:
"Some Words 1234"
Run Code Online (Sandbox Code Playgroud)
但我检索以下内容:
"Some Words 12 34"
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
更多例子:
Input: "Some Words That Should not be replaced 12 9 123 4 12"
Output: "Some Words That Should not be replaced 129123412"
Input: "test 9 8"
Output: "test 98"
Input: "t e s t 9 8"
Output: "t e s t 98"
Input: "Another 12 000"
Output: "Another 12000"
Run Code Online (Sandbox Code Playgroud) 假设以下字符串:
aaa bbb ccc
bbb aaa ccc
Run Code Online (Sandbox Code Playgroud)
aaa只要它不在字符串的开头,我想匹配.我试图通过这样做来否定它:
[^^]aaa
Run Code Online (Sandbox Code Playgroud)
但我不认为这是对的.用preg_replace.
搜索word未跟随@符号的字符串的正则表达式是什么?
例如:
mywordLLD OK
myword.dff OK
myword@ld Exclude
Run Code Online (Sandbox Code Playgroud) regex ×9
javascript ×5
escaping ×2
awk ×1
binary ×1
c# ×1
parsing ×1
python ×1
python-3.x ×1
quotes ×1
r ×1
replace ×1
sed ×1
unix ×1
validation ×1