我需要正则表达式方面的帮助。我有像"testBla"和之类"Bla"的词test"。
我想"test"从"testBla". 所以它应该只删除"test"给定字符串大于 4 个字符的情况。这就是我所拥有的:
^test\w{4,}
但它不起作用。
有任何想法吗?
这个只会捕获一个单词中的“测试”部分:\btest(?=\w{4,}). 我假设您使用的正则表达式引擎的前瞻长度为零。
假设您使用的是 JavaScript,请尝试以下操作:
string.replace(/test([^]+)/i, "$1");
'Bla'.replace(/test([^]+)/i, "$1"); // 'Bla'
'test'.replace(/test([^]+)/i, "$1"); // 'test'
'testBla'.replace(/test([^]+)/i, "$1"); // 'Bla'
'blaTest'.replace(/test([^]+)/i, "$1"); // 'blaTest'
'blaTestbla'.replace(/test([^]+)/i, "$1"); // 'blaTestbla'
这将从字符串中删除test,仅当字符串以 开头时test,并且仅当字符串中的内容多于 时test。我添加i了使正则表达式不区分大小写。