1

Da Sie in JavaScript keine negativen Lookbehinds verwenden können, wie würden Sie die gleiche Regex-Übereinstimmung erreichen, wenn Sie Lookahead oder eine andere JavaScript-unterstützte Methode verwenden?Wie erreicht man einen negativen Lookbehind mit einem Lookahead in Javascript?

(?<!<\/strong>\n\s{4}|<\/strong>&nbsp;\n\s{4})<br>

Ich versuche, alle <br>-Tags zu finden, die durch einen Zeilenumbruch durch </strong> und </strong>&nbsp; folgte nicht vorangehen und eine Reihe von Leerzeichen Zeichen.

here's my code und einige Beispieltext zum Testen. Ich habe es in pcre mit dem Lookbehind funktioniert, aber ich kann nicht herausfinden, wie es in JavaScript funktioniert.

Ich habe die anderen Beispiele hier ausprobiert, aber ich konnte nicht herausfinden, wie ich diese Lösungen nach meinen Bedürfnissen implementieren kann.

+0

[Ihr Versuch wird die Sprache nicht passender Sie planen, es zu benutzen, in] (https://regex101.com/r/ZactBT/2). –

+0

ja, ich habe es in pcre erstellt, bevor ich realisiert habe, dass ich nicht den gleichen Ausdruck in js verwenden konnte. jetzt versuche ich, den funktionierenden pcre-code in etwas js-kompatibles umzuwandeln. – selfagency

+0

Versuchen Sie, die Brs zu ersetzen? – KevBot

Antwort

1

Ein nicht-regulärer Ausdruck Ansatz

Wenn ich HTML analysiere, möchte ich HTML in DocumentFragments konvertieren. Dies ermöglicht mir die Verwendung von gemeinsamen querySelector-Methoden und JS-Funktionen, um mein Endziel zu erreichen.

Ich ersetze die 3 br Tags mit einem der folgenden: <div>REPLACED</div>. Um das Fragment zu erstellen, habe ich auch das npm-Paket html-fragment verwendet.

const html = `<p><strong>It was a weak bit.</strong>&nbsp; 
 
    <br>Because it&rsquo;s already been done. If he had been like, &ldquo;I don&rsquo;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?&rdquo; I would be like, &ldquo;Haa, what?! Okay.&rdquo; I mean, something new is going on here. He basically did the equivalent of like when guys do &ldquo;fat guy in a little coat,&rdquo; and they act like it&rsquo;s not a Chris Farley callback. It&rsquo;s like, this is a joke that&rsquo;s in our system, so you&rsquo;re a little bit unoriginal or even worse, you don&rsquo;t know you&rsquo;re unoriginal.&nbsp;</p> 
 
<p><strong>Does he know it&rsquo;s you?</strong>&nbsp; 
 
    <br>I do think he knew that I was a comedian named Jenny. So anyway, he&rsquo;s like, &ldquo;Let&rsquo;s go to the Renaissance fair,&rdquo; and I call my friends, and I&rsquo;m like, &ldquo;I&rsquo;m not going on this date.&rdquo; And they&rsquo;re like, &ldquo;Oh Jenny, come on, don&rsquo;t be so closed down, you need to get out there.&rdquo; I&rsquo;m like, &ldquo;UGH, fine.&rdquo; Then we have a series of text messages back and forth that I&rsquo;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&rsquo;m like, I don&rsquo;t know, maybe I just don&rsquo;t know what&rsquo;s going on. And he&rsquo;s asking me these questions that I&rsquo;m like, What. The. Fuck. Why don&rsquo;t you just wait?</p> 
 
<p><strong>What is he asking you?</strong>&nbsp; 
 
    <br>Like, &ldquo;Where was the last place you flew on an airplane?&rdquo; And I&rsquo;m not a rude or cruel woman, but I was like, I don&rsquo;t... dude... just wait. Sit me down, I&rsquo;ll tell you anything, just wait. Just wait until Saturday.</p> 
 
<p><strong>Also, that&rsquo;s the kind of question that is like, &ldquo;I&rsquo;m gonna go on the internet and search random questions to ask someone.&rdquo;</strong>&nbsp; 
 
    <br>Yeah, it&rsquo;s not great. It&rsquo;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&rsquo;m like, &ldquo;I made us a reservation at this restaurant, will you meet me there?&rdquo; He&rsquo;s like, &ldquo;Yes, is it fancy?&rdquo; I&rsquo;m like, &ldquo;No,&rdquo; and he&rsquo;s like, &ldquo;Okay, should I wear something like this?&rdquo; And he sends me a picture of a knight&rsquo;s costume. Like from the Renaissance fair. At which point I&rsquo;m like what the fuck, dude? Because I didn&rsquo;t even jump on this riff in the first place. It&rsquo;s not like I was like, &ldquo;Yes, and I will wear my wench&#39;s costume and bring a cup of mead!&rdquo; I&rsquo;m just like... Heh?! What? Why are you doing this?</p> 
 
<p><strong>You didn&rsquo;t respond positively to the bit.</strong> 
 
    <br>Never. Anyway, on the day of the date, I was like, &ldquo;How will I know it&rsquo;s you?&rdquo; And I thought he would be like, &ldquo;I&rsquo;m 6&rsquo;1&rdquo; and I&rsquo;ve got a beard,&rdquo; 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/[email protected]/lib/html-fragment.min.js"></script>

+0

Wenn ich diese Route gehe, dann benutze ich lieber jquery oder cheerio '$ ('br'). Je (function() {if (! $ (This) .siblings ('strong')) $ (this) .remove()}) ' – selfagency

+0

@selfagency, also ist die allgemeine Idee, Br-Tags zu entfernen, die das einzige Kind von p-Tags sind? – KevBot

0

Sie müssen es übereinstimmen, um vorbei zu gehen.
Es gibt keinen anderen Weg !!!!

/(<\/strong>\n\s{4}|<\/strong>&nbsp;\n\s{4})?<br>/

Innerhalb eines Rückrufs, wenn Gruppe 1 nicht null ist

https://regex101.com/r/xVJhGl/1

var text = 
 
"<p><strong>It was a weak bit.</strong>&nbsp;\n" 
 
+ " <br>Because it&rsquo;s already been done. If he had been like, &ldquo;I don&rsquo;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?&rdquo; I would be like, &ldquo;Haa, what?! Okay.&rdquo; I mean, something new is going on here. He basically did the equivalent of like when guys do &ldquo;fat guy in a little coat,&rdquo; and they act like it&rsquo;s not a Chris Farley callback. It&rsquo;s like, this is a joke that&rsquo;s in our system, so you&rsquo;re a little bit unoriginal or even worse, you don&rsquo;t know you&rsquo;re unoriginal.&nbsp;</p>\n" 
 
+ "<p><strong>Does he know it&rsquo;s you?</strong>&nbsp;\n" 
 
+ " <br>I do think he knew that I was a comedian named Jenny. So anyway, he&rsquo;s like, &ldquo;Let&rsquo;s go to the Renaissance fair,&rdquo; and I call my friends, and I&rsquo;m like, &ldquo;I&rsquo;m not going on this date.&rdquo; And they&rsquo;re like, &ldquo;Oh Jenny, come on, don&rsquo;t be so closed down, you need to get out there.&rdquo; I&rsquo;m like, &ldquo;UGH, fine.&rdquo; Then we have a series of text messages back and forth that I&rsquo;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&rsquo;m like, I don&rsquo;t know, maybe I just don&rsquo;t know what&rsquo;s going on. And he&rsquo;s asking me these questions that I&rsquo;m like, What. The. Fuck. Why don&rsquo;t you just wait?</p>\n" 
 
+ "<p><strong>What is he asking you?</strong>&nbsp;\n" 
 
+ " <br>Like, &ldquo;Where was the last place you flew on an airplane?&rdquo; And I&rsquo;m not a rude or cruel woman, but I was like, I don&rsquo;t... dude... just wait. Sit me down, I&rsquo;ll tell you anything, just wait. Just wait until Saturday.</p>\n" 
 
+ "<p><strong>Also, that&rsquo;s the kind of question that is like, &ldquo;I&rsquo;m gonna go on the internet and search random questions to ask someone.&rdquo;</strong>&nbsp;\n" 
 
+ " <br>Yeah, it&rsquo;s not great. It&rsquo;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&rsquo;m like, &ldquo;I made us a reservation at this restaurant, will you meet me there?&rdquo; He&rsquo;s like, &ldquo;Yes, is it fancy?&rdquo; I&rsquo;m like, &ldquo;No,&rdquo; and he&rsquo;s like, &ldquo;Okay, should I wear something like this?&rdquo; And he sends me a picture of a knight&rsquo;s costume. Like from the Renaissance fair. At which point I&rsquo;m like what the fuck, dude? Because I didn&rsquo;t even jump on this riff in the first place. It&rsquo;s not like I was like, &ldquo;Yes, and I will wear my wench&#39;s costume and bring a cup of mead!&rdquo; I&rsquo;m just like... Heh?! What? Why are you doing this?</p>\n" 
 
+ "<p><strong>You didn&rsquo;t respond positively to the bit.</strong>\n" 
 
+ " <br>Never. Anyway, on the day of the date, I was like, &ldquo;How will I know it&rsquo;s you?&rdquo; And I thought he would be like, &ldquo;I&rsquo;m 6&rsquo;1&rdquo; and I&rsquo;ve got a beard,&rdquo; or some\n" 
 
+ "<p><br></p>\n" 
 
+ "<p><br></p>\n" 
 
+ "<p><br></p>\n" 
 
; 
 

 
var rx = /(<\/strong>\n\s{4}|<\/strong>&nbsp;\n\s{4})?<br>/g; 
 

 
text = text.replace(rx , function(match, a) 
 
    { 
 
     if (a) 
 
      return match; 
 
     return "<REPL>"; 
 
    } 
 
); 
 

 
console.log(text);

+0

danke. Das war auch das weiteste, was ich in js vs. pcre versucht habe. Ich nehme an, es ist vielleicht meine beste Option, einfach eine Funktion off .replace() zu erstellen, um diesen Fall zu ignorieren, anstatt zu versuchen, alles in der Regex zu tun. – selfagency

+0

@selfagency - Posted ein Beispiel Verwendung. – sln

Verwandte Themen