正则表达式:匹配文本段落中的所有内容,特定短语除外
Regex: Match everything in text paragraph except specific phrases
我正在为 Google 文档和处理文本段落编写一个免费插件。
我需要一个正则表达式来匹配除 短语 之外的所有内容(即用空格分隔的多个单词)。
例如,在搜索文本 The quick brown fox jumped over the lazy dog
时,我想匹配除 quick brown
和 lazy
之外的所有内容,预期结果为 The fox jumped over the dog
.
\b((?!(lazy)\b).)+
这行得通;它匹配除 lazy
之外的所有文本,我得到 The quick brown fox jumped over the dog
.
\b((?!(quick brown|lazy)\b).)+
这是行不通的;它留在 brown
中,当我应该得到 The fox jumped over the dog
时,我得到 The brown fox jumped over the dog
我已经在网上搜索了好几个小时,但一无所获。正则表达式缺少一些东西,我不知道它是什么。
感谢阅读!
正则表达式示例:https://regex101.com/r/3HGiff/1
Javascript 示例:https://jsfiddle.net/g85je2aj/16/
EDIT/update: 我开发了另一个解决方案,但它依赖于正后视,只有 Chrome 支持。
((?<=(quick brown|lazy)+(?=[\s]))|^(?!(quick brown|lazy))).+?((?=(quick brown|lazy))|$)
正则表达式示例:https://regex101.com/r/3HGiff/3
Javascript 示例:https://jsfiddle.net/g85je2aj/19/
因为这只适用于 Chrome,我认为这不是真正的解决方案。关于如何修改该正则表达式以不使用回顾的任何想法,或者这是不可能的?
或者您可以改用捕获组:
(.*)(one|two words)\s(.*)
然后您可以使用以下方法获取没有指定单词的文本:
。
示例: regex101.com
您可以使用 拆分 方法,而不是匹配所有与某些字符串不匹配的文本。您可以使用一个短语列表来避免构建基于交替的正则表达式并将其与 String#split()
:
一起使用
var regExp = new RegExp("\b(?:" + phrasesToSearchFor + ")\b","i");
var results = textToSearchIn.split(regExp);
您稍后需要做的就是访问 results
数组中的所有项目。
这是 JS 演示:
$(document).ready(function() {
$("#button").click(function () {
//the text to search for words in, then inverse highlight
var textToSearchIn = "The quick brown fox jumped over the lazy dog.";
//phrases to search for in a regex-friendly format
//please note: this string vary in length and number of phrases
// as it is parsed from an array of phrases using array.join('|');
var phrasesToSearchFor = "quick brown|lazy";
//build a new regular expression to match everything but the phrasesToSearchFor
//the best regex I have figured out is: \b((?!(quick brown|lazy)\b).)+
//but it only works for single-word phrases
var regExp = new RegExp("\b(?:" + phrasesToSearchFor + ")\b","i");
//do a while loop to collect all the matches
var results = textToSearchIn.split(regExp);
for (var result of results) {
//format the matche as a list item. we only need the first group [0]
var result = $('<li>' + result + '</li>');
//send the match to the html list
$('#output').before(result);
}
/* expected output:
* The
* fox jumped over the
* dog.
actual output:
* The
* brown fox jumped over the
* dog.
*/
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Click to test</button>
<ul id="output"></ul>
我正在为 Google 文档和处理文本段落编写一个免费插件。
我需要一个正则表达式来匹配除 短语 之外的所有内容(即用空格分隔的多个单词)。
例如,在搜索文本 The quick brown fox jumped over the lazy dog
时,我想匹配除 quick brown
和 lazy
之外的所有内容,预期结果为 The fox jumped over the dog
.
\b((?!(lazy)\b).)+
这行得通;它匹配除 lazy
之外的所有文本,我得到 The quick brown fox jumped over the dog
.
\b((?!(quick brown|lazy)\b).)+
这是行不通的;它留在 brown
中,当我应该得到 The fox jumped over the dog
The brown fox jumped over the dog
我已经在网上搜索了好几个小时,但一无所获。正则表达式缺少一些东西,我不知道它是什么。
感谢阅读!
正则表达式示例:https://regex101.com/r/3HGiff/1
Javascript 示例:https://jsfiddle.net/g85je2aj/16/
EDIT/update: 我开发了另一个解决方案,但它依赖于正后视,只有 Chrome 支持。
((?<=(quick brown|lazy)+(?=[\s]))|^(?!(quick brown|lazy))).+?((?=(quick brown|lazy))|$)
正则表达式示例:https://regex101.com/r/3HGiff/3
Javascript 示例:https://jsfiddle.net/g85je2aj/19/
因为这只适用于 Chrome,我认为这不是真正的解决方案。关于如何修改该正则表达式以不使用回顾的任何想法,或者这是不可能的?
或者您可以改用捕获组:
(.*)(one|two words)\s(.*)
然后您可以使用以下方法获取没有指定单词的文本:。
示例: regex101.com
您可以使用 拆分 方法,而不是匹配所有与某些字符串不匹配的文本。您可以使用一个短语列表来避免构建基于交替的正则表达式并将其与 String#split()
:
var regExp = new RegExp("\b(?:" + phrasesToSearchFor + ")\b","i");
var results = textToSearchIn.split(regExp);
您稍后需要做的就是访问 results
数组中的所有项目。
这是 JS 演示:
$(document).ready(function() {
$("#button").click(function () {
//the text to search for words in, then inverse highlight
var textToSearchIn = "The quick brown fox jumped over the lazy dog.";
//phrases to search for in a regex-friendly format
//please note: this string vary in length and number of phrases
// as it is parsed from an array of phrases using array.join('|');
var phrasesToSearchFor = "quick brown|lazy";
//build a new regular expression to match everything but the phrasesToSearchFor
//the best regex I have figured out is: \b((?!(quick brown|lazy)\b).)+
//but it only works for single-word phrases
var regExp = new RegExp("\b(?:" + phrasesToSearchFor + ")\b","i");
//do a while loop to collect all the matches
var results = textToSearchIn.split(regExp);
for (var result of results) {
//format the matche as a list item. we only need the first group [0]
var result = $('<li>' + result + '</li>');
//send the match to the html list
$('#output').before(result);
}
/* expected output:
* The
* fox jumped over the
* dog.
actual output:
* The
* brown fox jumped over the
* dog.
*/
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="button">Click to test</button>
<ul id="output"></ul>