如何在 javascript 中使用前瞻来实现负向后回顾?
how to achieve a negative lookbehind using a lookahead in javascript?
由于您不能在 JavaScript 中使用负向后视,如果不使用前向或其他 JavaScript 支持的方法,您如何实现相同的正则表达式匹配?
(?<!<\/strong>\n\s{4}|<\/strong> \n\s{4})<br>
我正在尝试查找所有 <br>
标记,这些标记前面没有 </strong>
和 </strong>
,后跟换行符和一系列空白字符。
Here's my code 和一些用于测试的示例文本。我让它在 pcre 中使用 lookbehind 但我不知道如何让它在 JavaScript.
中工作
我已经尝试了这里的其他示例,但无法弄清楚如何根据我的需要实施这些解决方案。
更新 Javascript 回顾是 coming in ES2018!
你必须匹配它才能通过它。
根本没有别的办法!!!!
/(<\/strong>\n\s{4}|<\/strong> \n\s{4})?<br>/
在回调中,如果组 1 不为空
https://regex101.com/r/xVJhGl/1
var text =
"<p><strong>It was a weak bit.</strong> \n"
+ " <br>Because it’s already been done. If he had been like, “I don’t know, do you want to go to the La Brea tar pits and scoop some tar out and give ourselves a facial and burn ourselves and then only be able to know each other?” I would be like, “Haa, what?! Okay.” I mean, something new is going on here. He basically did the equivalent of like when guys do “fat guy in a little coat,” and they act like it’s not a Chris Farley callback. It’s like, this is a joke that’s in our system, so you’re a little bit unoriginal or even worse, you don’t know you’re unoriginal. </p>\n"
+ "<p><strong>Does he know it’s you?</strong> \n"
+ " <br>I do think he knew that I was a comedian named Jenny. So anyway, he’s like, “Let’s go to the Renaissance fair,” and I call my friends, and I’m like, “I’m not going on this date.” And they’re like, “Oh Jenny, come on, don’t be so closed down, you need to get out there.” I’m like, “UGH, fine.” Then we have a series of text messages back and forth that I’m just kind of like, <em>What is this? Is this what dating is like?</em> I was with my ex-husband for nine years, then I was in a very serious relationship that was passionate for a year, and I’m like, I don’t know, maybe I just don’t know what’s going on. And he’s asking me these questions that I’m like, What. The. Fuck. Why don’t you just wait?</p>\n"
+ "<p><strong>What is he asking you?</strong> \n"
+ " <br>Like, “Where was the last place you flew on an airplane?” And I’m not a rude or cruel woman, but I was like, I don’t... dude... just wait. Sit me down, I’ll tell you anything, just wait. Just wait until Saturday.</p>\n"
+ "<p><strong>Also, that’s the kind of question that is like, “I’m gonna go on the internet and search random questions to ask someone.”</strong> \n"
+ " <br>Yeah, it’s not great. It’s a real speed-dating question. I would love it in other circumstances if it was like a page in <em>Entertainment Weekly</em>, you know? So then I’m like, “I made us a reservation at this restaurant, will you meet me there?” He’s like, “Yes, is it fancy?” I’m like, “No,” and he’s like, “Okay, should I wear something like this?” And he sends me a picture of a knight’s costume. Like from the Renaissance fair. At which point I’m like what the fuck, dude? Because I didn’t even jump on this riff in the first place. It’s not like I was like, “Yes, and I will wear my wench's costume and bring a cup of mead!” I’m just like... Heh?! What? Why are you doing this?</p>\n"
+ "<p><strong>You didn’t respond positively to the bit.</strong>\n"
+ " <br>Never. Anyway, on the day of the date, I was like, “How will I know it’s you?” And I thought he would be like, “I’m 6’1” and I’ve got a beard,” or some\n"
+ "<p><br></p>\n"
+ "<p><br></p>\n"
+ "<p><br></p>\n"
;
var rx = /(<\/strong>\n\s{4}|<\/strong> \n\s{4})?<br>/g;
text = text.replace( rx , function(match, a)
{
if ( a )
return match;
return "<REPL>";
}
);
console.log( text );
非正则表达式方法
解析HTML时,我更喜欢将HTML转换成DocumentFragments。这允许我使用常见的 querySelector 方法和 JS 函数来实现我的最终目标。
我将 3 个 br 标签替换为以下内容:<div>REPLACED</div>
。另外,为了创建片段,我使用了 npm 包 html-fragment
.
const html = `<p><strong>It was a weak bit.</strong>
<br>Because it’s already been done. If he had been like, “I don’t know, do you want to go to the La Brea tar pits and scoop some tar out and give ourselves a facial and burn ourselves and then only be able to know each other?” I would be like, “Haa, what?! Okay.” I mean, something new is going on here. He basically did the equivalent of like when guys do “fat guy in a little coat,” and they act like it’s not a Chris Farley callback. It’s like, this is a joke that’s in our system, so you’re a little bit unoriginal or even worse, you don’t know you’re unoriginal. </p>
<p><strong>Does he know it’s you?</strong>
<br>I do think he knew that I was a comedian named Jenny. So anyway, he’s like, “Let’s go to the Renaissance fair,” and I call my friends, and I’m like, “I’m not going on this date.” And they’re like, “Oh Jenny, come on, don’t be so closed down, you need to get out there.” I’m like, “UGH, fine.” Then we have a series of text messages back and forth that I’m just kind of like, <em>What is this? Is this what dating is like?</em> I was with my ex-husband for nine years, then I was in a very serious relationship that was passionate for a year, and I’m like, I don’t know, maybe I just don’t know what’s going on. And he’s asking me these questions that I’m like, What. The. Fuck. Why don’t you just wait?</p>
<p><strong>What is he asking you?</strong>
<br>Like, “Where was the last place you flew on an airplane?” And I’m not a rude or cruel woman, but I was like, I don’t... dude... just wait. Sit me down, I’ll tell you anything, just wait. Just wait until Saturday.</p>
<p><strong>Also, that’s the kind of question that is like, “I’m gonna go on the internet and search random questions to ask someone.”</strong>
<br>Yeah, it’s not great. It’s a real speed-dating question. I would love it in other circumstances if it was like a page in <em>Entertainment Weekly</em>, you know? So then I’m like, “I made us a reservation at this restaurant, will you meet me there?” He’s like, “Yes, is it fancy?” I’m like, “No,” and he’s like, “Okay, should I wear something like this?” And he sends me a picture of a knight’s costume. Like from the Renaissance fair. At which point I’m like what the fuck, dude? Because I didn’t even jump on this riff in the first place. It’s not like I was like, “Yes, and I will wear my wench's costume and bring a cup of mead!” I’m just like... Heh?! What? Why are you doing this?</p>
<p><strong>You didn’t respond positively to the bit.</strong>
<br>Never. Anyway, on the day of the date, I was like, “How will I know it’s you?” And I thought he would be like, “I’m 6’1” and I’ve got a beard,” or some
<p><br></p>
<p><br></p>
<p><br></p>`;
const fragment = HtmlFragment(html);
Array.from(fragment.querySelectorAll('br'))
.filter(br => {
let previous = br.previousElementSibling;
return (previous === null || previous.nodeName !== 'STRONG');
})
.forEach(br => {
let div = document.createElement('div');
div.innerText = 'REPLACED';
br.parentNode.replaceChild(div, br);
});
let div = document.createElement('div');
div.appendChild(fragment);
console.log(div.innerHTML);
<script src="https://unpkg.com/html-fragment@1.1.0/lib/html-fragment.min.js"></script>
由于您不能在 JavaScript 中使用负向后视,如果不使用前向或其他 JavaScript 支持的方法,您如何实现相同的正则表达式匹配?
(?<!<\/strong>\n\s{4}|<\/strong> \n\s{4})<br>
我正在尝试查找所有 <br>
标记,这些标记前面没有 </strong>
和 </strong>
,后跟换行符和一系列空白字符。
Here's my code 和一些用于测试的示例文本。我让它在 pcre 中使用 lookbehind 但我不知道如何让它在 JavaScript.
中工作我已经尝试了这里的其他示例,但无法弄清楚如何根据我的需要实施这些解决方案。
更新 Javascript 回顾是 coming in ES2018!
你必须匹配它才能通过它。
根本没有别的办法!!!!
/(<\/strong>\n\s{4}|<\/strong> \n\s{4})?<br>/
在回调中,如果组 1 不为空
https://regex101.com/r/xVJhGl/1
var text =
"<p><strong>It was a weak bit.</strong> \n"
+ " <br>Because it’s already been done. If he had been like, “I don’t know, do you want to go to the La Brea tar pits and scoop some tar out and give ourselves a facial and burn ourselves and then only be able to know each other?” I would be like, “Haa, what?! Okay.” I mean, something new is going on here. He basically did the equivalent of like when guys do “fat guy in a little coat,” and they act like it’s not a Chris Farley callback. It’s like, this is a joke that’s in our system, so you’re a little bit unoriginal or even worse, you don’t know you’re unoriginal. </p>\n"
+ "<p><strong>Does he know it’s you?</strong> \n"
+ " <br>I do think he knew that I was a comedian named Jenny. So anyway, he’s like, “Let’s go to the Renaissance fair,” and I call my friends, and I’m like, “I’m not going on this date.” And they’re like, “Oh Jenny, come on, don’t be so closed down, you need to get out there.” I’m like, “UGH, fine.” Then we have a series of text messages back and forth that I’m just kind of like, <em>What is this? Is this what dating is like?</em> I was with my ex-husband for nine years, then I was in a very serious relationship that was passionate for a year, and I’m like, I don’t know, maybe I just don’t know what’s going on. And he’s asking me these questions that I’m like, What. The. Fuck. Why don’t you just wait?</p>\n"
+ "<p><strong>What is he asking you?</strong> \n"
+ " <br>Like, “Where was the last place you flew on an airplane?” And I’m not a rude or cruel woman, but I was like, I don’t... dude... just wait. Sit me down, I’ll tell you anything, just wait. Just wait until Saturday.</p>\n"
+ "<p><strong>Also, that’s the kind of question that is like, “I’m gonna go on the internet and search random questions to ask someone.”</strong> \n"
+ " <br>Yeah, it’s not great. It’s a real speed-dating question. I would love it in other circumstances if it was like a page in <em>Entertainment Weekly</em>, you know? So then I’m like, “I made us a reservation at this restaurant, will you meet me there?” He’s like, “Yes, is it fancy?” I’m like, “No,” and he’s like, “Okay, should I wear something like this?” And he sends me a picture of a knight’s costume. Like from the Renaissance fair. At which point I’m like what the fuck, dude? Because I didn’t even jump on this riff in the first place. It’s not like I was like, “Yes, and I will wear my wench's costume and bring a cup of mead!” I’m just like... Heh?! What? Why are you doing this?</p>\n"
+ "<p><strong>You didn’t respond positively to the bit.</strong>\n"
+ " <br>Never. Anyway, on the day of the date, I was like, “How will I know it’s you?” And I thought he would be like, “I’m 6’1” and I’ve got a beard,” or some\n"
+ "<p><br></p>\n"
+ "<p><br></p>\n"
+ "<p><br></p>\n"
;
var rx = /(<\/strong>\n\s{4}|<\/strong> \n\s{4})?<br>/g;
text = text.replace( rx , function(match, a)
{
if ( a )
return match;
return "<REPL>";
}
);
console.log( text );
非正则表达式方法
解析HTML时,我更喜欢将HTML转换成DocumentFragments。这允许我使用常见的 querySelector 方法和 JS 函数来实现我的最终目标。
我将 3 个 br 标签替换为以下内容:<div>REPLACED</div>
。另外,为了创建片段,我使用了 npm 包 html-fragment
.
const html = `<p><strong>It was a weak bit.</strong>
<br>Because it’s already been done. If he had been like, “I don’t know, do you want to go to the La Brea tar pits and scoop some tar out and give ourselves a facial and burn ourselves and then only be able to know each other?” I would be like, “Haa, what?! Okay.” I mean, something new is going on here. He basically did the equivalent of like when guys do “fat guy in a little coat,” and they act like it’s not a Chris Farley callback. It’s like, this is a joke that’s in our system, so you’re a little bit unoriginal or even worse, you don’t know you’re unoriginal. </p>
<p><strong>Does he know it’s you?</strong>
<br>I do think he knew that I was a comedian named Jenny. So anyway, he’s like, “Let’s go to the Renaissance fair,” and I call my friends, and I’m like, “I’m not going on this date.” And they’re like, “Oh Jenny, come on, don’t be so closed down, you need to get out there.” I’m like, “UGH, fine.” Then we have a series of text messages back and forth that I’m just kind of like, <em>What is this? Is this what dating is like?</em> I was with my ex-husband for nine years, then I was in a very serious relationship that was passionate for a year, and I’m like, I don’t know, maybe I just don’t know what’s going on. And he’s asking me these questions that I’m like, What. The. Fuck. Why don’t you just wait?</p>
<p><strong>What is he asking you?</strong>
<br>Like, “Where was the last place you flew on an airplane?” And I’m not a rude or cruel woman, but I was like, I don’t... dude... just wait. Sit me down, I’ll tell you anything, just wait. Just wait until Saturday.</p>
<p><strong>Also, that’s the kind of question that is like, “I’m gonna go on the internet and search random questions to ask someone.”</strong>
<br>Yeah, it’s not great. It’s a real speed-dating question. I would love it in other circumstances if it was like a page in <em>Entertainment Weekly</em>, you know? So then I’m like, “I made us a reservation at this restaurant, will you meet me there?” He’s like, “Yes, is it fancy?” I’m like, “No,” and he’s like, “Okay, should I wear something like this?” And he sends me a picture of a knight’s costume. Like from the Renaissance fair. At which point I’m like what the fuck, dude? Because I didn’t even jump on this riff in the first place. It’s not like I was like, “Yes, and I will wear my wench's costume and bring a cup of mead!” I’m just like... Heh?! What? Why are you doing this?</p>
<p><strong>You didn’t respond positively to the bit.</strong>
<br>Never. Anyway, on the day of the date, I was like, “How will I know it’s you?” And I thought he would be like, “I’m 6’1” and I’ve got a beard,” or some
<p><br></p>
<p><br></p>
<p><br></p>`;
const fragment = HtmlFragment(html);
Array.from(fragment.querySelectorAll('br'))
.filter(br => {
let previous = br.previousElementSibling;
return (previous === null || previous.nodeName !== 'STRONG');
})
.forEach(br => {
let div = document.createElement('div');
div.innerText = 'REPLACED';
br.parentNode.replaceChild(div, br);
});
let div = document.createElement('div');
div.appendChild(fragment);
console.log(div.innerHTML);
<script src="https://unpkg.com/html-fragment@1.1.0/lib/html-fragment.min.js"></script>