請不要關閉問題。我已經在J??avaScript和javascript 正則運算式中經歷了Negative lookbehind 等價物- 尋找替代方案?但我無法獲得正確的正則運算式。我真的嘗試過自己,但沒有成功。為了得到答案,我進行了多次試驗。由于不成功,因此發布了這個問題。
我有一個正則運算式。它在 Chrome 瀏覽器中運行良好,但我在 Safari 和 IOS 中遇到問題。問題是消極的后視。有沒有辦法消除或模仿負面的后視,以便相同的運算式可以在所有 3 個環境中作業?
const regex = /(?<!\[)@([\w.'-] (?:\s[\w.'-] )?)/g;
我正在使用上面的正則運算式來執行以下功能。字串資料是從文本區域動態接收的。
有沒有其他方法可以在沒有正則運算式的情況下獲得所需的結果?
const stringData = "[@Shilpa Shetty] [@Rohit Shetty] [@Rohit Bahl] @Salman Khan [@Sonal Shetty][@Mary James], [@Jennifer John] and [@Johnny Lever[@Patricia Robert] are present in the meeting and [@Jerry[@Jeffery Roger] is absent.";
while ((match = regex2.exec(stringData)) !== null) {
console.log('Starting Index', match.index);
console.log('Ending Index', regex2.lastIndex);
}
我附上了兩個演示。
這個要在chrome中打開并測驗=> https://regex101.com/r/DUbiiK/1
這個要在 safari 中打開并測驗 => https://regexr.com/6gl48
需要幫助,因為正則運算式對我來說是非常新的東西,我已經嘗試了多次,但無法得到想要的結果。
uj5u.com熱心網友回復:
我會去做這樣的事情:
/(?:^|[^\[])@([\w.'-] (?:\s[\w.'-] )?)/g
(?:^|[^\[])- 以行首或非[字符開頭@([\w.'-] (?:\s[\w.'-] )?)- 不變。我想你知道你寫了什么...
const regex = /(?:^|[^\[])@([\w.'-] (?:\s[\w.'-] )?)/g;
const stringData = "@Salman Khan [@Shilpa Shetty] [@Rohit Shetty] [@Rohit Bahl] @Salman Khan [@Sonal Shetty][@Mary James], [@Jennifer John] and [@Johnny Lever[@Patricia Robert] are present in the meeting and [@Jerry[@Jeffery Roger] is absent.";
while ((match = regex.exec(stringData)) !== null) {
// If we've matched something at the start of the line then don't compensate for the superfluous char before @
// If something was matched in the middle of the string then compensate for whatever preceded the `@`
console.log('Starting Index', (match.index < 1 ? match.index : (match.index 1)));
console.log('Ending Index', regex.lastIndex);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/437246.html
標籤:javascript 反应 正则表达式
上一篇:正則運算式模式匹配錯誤的字串
下一篇:多行注釋flexlex
