【发布时间】:2020-10-17 11:06:11
【问题描述】:
您好,我有一个要求,我想基于此形成一个正则表达式
- 如果它以正则表达式中的给定单词开头,则匹配整个单词
- 匹配整个双引号括起来的单词
- 如果单词以正则表达式中的给定单词开头,则匹配 (..)、[..]、{..} 中的单词 - 将类似于 (1),但括号内除外
这是我如何形成正则表达式
-
对于 1)
\s*(pattern)\s*\S演示:https://regex101.com/r/dFHTtv/2 -
对于 2) 和 3)
(?<=[\(|\[|"])\s*(virat kohli|virat|kohli)\s*(?=([\]|\)|"]))演示:https://regex101.com/r/aOox5A/3
我想把它们结合起来,让它完美地工作
这是我尝试过的
var searchString = `virat "virat kohli" regarded ipl`;
var keywordList = searchString.split(/\s+/g);
var exactSearchList = searchString.match(/(?<=").*?(?=")/g);
var textDescription = `<pre>Virat Kohli (About this soundpronunciation (help·info); born 5 November 1988) is an Indian cricketer and the current captain of the India national team. A right-handed top-order batsman, Kohli is regarded as one of the best batsmen in the world.[3] He plays for Royal Challengers Bangalore in the Indian Premier League (IPL), and has been the team's captain since 2013. Since October 2017, he has been the top-ranked ODI batsman in the world and is currently 2nd in Test rankings with 886 points.[4][5] Among Indian batsmen, Kohli has the best ever Test rating (937 points), ODI rating (911 points) and T20I rating (897 points).
virat kohli is one of best performer in ipl
(virat ) (kohli)
kohli is good player virat virat kohli kohli
custom kohli virat virat kohli kohli virat "virat kohli"
"virat kohli" "kohli virat" "virat"
"kohli"
Kohli captained India Under-19s to victory at the 2008 Under-19 World Cup in Malaysia. After a few months later, he made his ODI debut for India against Sri Lanka at the age of 19. Initially having played as a reserve batsman in the Indian team, he soon established himself as a regular in the ODI middle-order and was part of the squad that won the 2011 World Cup. He made his Test debut in 2011 and shrugged off the tag of "ODI specialist" by 2013 with Test hundreds in Australia and South Africa.[6] Having reached the number one spot in the ICC rankings for ODI batsmen for the first time in 2013,[7] Kohli also found success in the Twenty20 format, winning the Man of the Tournament twice at the ICC World Twenty20 (in 2014 and 2016).
Kohli was appointed the vice-captain of the ODI team in 2012 and handed over the Test captaincy following Mahendra Singh Dhoni's Test retirement in 2014. In early 2017, he became the limited-overs captain as well after Dhoni stepped down from the position. In ODIs, Kohli has the second highest number of centuries and the highest number of centuries in run-chases in the world. He holds the world record for being the fastest batsman to 8,000, 9,000, 10,000 and 11,000 runs in ODI cricket, reaching the milestones in 175, 194, 205 and 222 innings respectively.[8][9] [10][11]
Kohli has been the recipient of many awards such as the Sir Garfield Sobers Trophy (ICC Cricketer of the Year) in 2017 and 2018; ICC Test Player of the Year 2018; ICC ODI Player of the Year in 2012, 2017 and 2018 and Wisden Leading Cricketer in the World in 2016, 2017 and 2018.[12] He was given the Arjuna Award in 2013, the Padma Shri under the sports category in 2017[13] and the Rajiv Gandhi Khel Ratna, the highest sporting honour in India, in 2018.[14] Kohli is ranked as one of the world's most famous athletes by ESPN[15] and one of the most valuable athlete brands by Forbes.[16] In 2018, Time magazine named Kohli one of the 100 most influential people in the world.[17] In 2020, Kohli was ranked 66th in Forbes list of the top 100 highest paid athletes in the world for the year 2020 with estimated earnings of $26 million. Virat Kohli is the only cricketer to feature in Forbes.[18]</pre>`;
var exactSearchListTemp = [];
for(var i = 0; i < exactSearchList.length; i++){
if(String(exactSearchList[i]).trim() != '')
exactSearchListTemp.push(String(exactSearchList[i]).trim());
}
if(Array.isArray(exactSearchList) && exactSearchList.length) keywordList = keywordList.concat(exactSearchListTemp);
var convertedRegex = keywordList.filter(function(el){
return el.replace(/\|/g,'');
}).join('|');
var pattern = new RegExp(`((?<=(^|\\s|^\\[?.*\\W]?|^\\()|^{?)(?:${convertedRegex}))\\S*`, 'ig');
var highlightDesc = textDescription.replace(pattern, '<highlight>$&</highlight>');
$('#matchedContainer').html(highlightDesc);
#matchedContainer{
border:1px solid #ccc;
width:100%;
height:400px;
}
highlight{
font-weight: 400;
background: #FF9800;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="matchedContainer"></div>
【问题讨论】:
-
试试
/(?<=[(["]\s*|\b)(virat(?: kohli)?|kohli)(?=\b|\s*[\])"])/gi,见demo #1。或者,/(?<=[(["]\s*|\b)(?:vir|koh|regar)\w*(?=\b|\s*[\])"])/gi,参见this demo #2。 -
似乎是匹配的,但我怎么能把它动态?
-
哪个版本有效?第一个?
-
@WiktorStribiżew,我想动态放置它,因为我的匹配文本也将来自输入字段和描述。我想动态地制作正则表达式。
-
@WiktorStribiżew,第一个完美匹配
标签: javascript jquery regex