我正在尝试在 JS 中使用以下正则表达式:
(?<=@[A-Z|a-z]+,)\s|(?<=@[A-Z|a-z]+,\s[A-Z|a-z]+)\s(?=\[[A-Z|a-z]+\])
翻译为:
匹配所有以 : 开头的空格
-
@
- 后跟
A-Z
范围内任意数量的字符或a-z
- 后跟一个逗号
或者
匹配前面的所有空格:
@
后跟
A-Z
范围内的任意数量的字符或a-z
- 后跟一个逗号
- 后跟一个空格
- 后跟
A-Z
范围内任意数量的字符或a-z
AND 后接:
-
[
- 后跟
A-Z
范围内任意数量的字符或a-z
-
]
但是,JS 不支持lookbehind。有没有其他方法可以在 JS 或任何我可以使用的 npm 库中支持上述正则表达式?
如果我们有一个像
这样的句子
Hi my name is @John, Doe [Example] and I am happy to be here
那应该变成
Hi my name is @John,Doe[Example] and I am happy to be here
.
另外,如果我们有类似的东西
Hi my name is @John, Smith Doe [Example]
,应该变成
Hi my name is @John,SmithDoe[Example]
.
最佳答案
我已更新了新输入的答案
console.clear();
var inputEl = document.querySelector('#input')
var outputEl = document.querySelector('#output')
function rep (e) {
var input = e.target.value;
var reg = /@([a-z]+?\s*?)+,(\s+[a-z]+)+(\s\[[a-z]+\])?/gim
matches = input.match(reg);
var output = input;
if (matches) {
replaceMap = new Map()
for (var i = 0; i < matches.length; i++) {
var m = matches[i]
.replace(/\[/, '\\[')
.replace(/\]/, '\\]')
replaceMap.set(m, matches[i].replace(/\s+/gm, ''))
}
for (var [s,r] of replaceMap) {
output = output.replace(new RegExp(s, 'gm'), r)
}
}
outputEl.textContent = output
}
inputEl.addEventListener('input', rep)
inputEl.dispatchEvent(new Event('input'))
textarea {
width: 100%;
min-height: 100px;
}
<h3>Input</h3>
<textarea id="input">@Lopez de la Cerda, Antonio Gabriel Hugo David [Author]. I'm the father of @Marquez, Maria</textarea>
<h3>Output (initially empty)</h3>
<p id="output"></p>
<h3>Expected result (on initial input)</h3>
<p>@LopezdelaCerda,AntonioGabrielHugoDavid[Author]. I'm the father of @Marquez,Maria</p>
旧答案内容的备份(出于历史原因)
至少在 Chrome 中使用此正则表达式可以工作:
/(?<=@[a-z]+,)\s+(?![a-z]+\s+\[[a-z]+\])|(?<=(@[a-z]+,\s[a-z]+))\s+(?=\[[a-z]+\])/gmi
参见:https://regex101.com/r/elTkRe/4
但是你不能在PCRE中使用它,因为它不允许在lookbehinds中使用量词。它们必须具有固定宽度。请参阅右侧的错误:https://regex101.com/r/ZC3XmX/2
没有向后看和向前看的解决方案
console.clear();
var reg = /(@[A-Za-z]+,\s[A-Za-z]+)(\s+)(\[[A-Za-z]+\])|(@[A-Z|a-z]+,)(\s+)/gm
var probes = [
'@gotAMatch, <<<',
'@LongerWithMatch, <<<',
'@MatchHereAsWell, <<<',
'@Yup, <<<<',
'@noMatchInThisLine,<<<<<',
'@match, match [match]<<<<<<<',
'@ noMatchInThisLine, <<<<'
]
for (var i in probes) {
console.log(probes[i].replace(reg, '$1$3$4'))
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - Javascript 替代方案中的正则表达式 Lookbehind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54253280/