Why did I have to add global research to this replace?

Asked

Viewed 41 times

2

I am trying to remove this string from my string ​.

var mypage = '<div pr-compile-html="d.article.body" id="article-body" pr-highlight="" pr-highlight-tooltip="#highlight-tooltip" highlight-class="highlight" class="ng-isolate-scope"><p class="ng-scope">Mussum Ipsum, cacilds vidis litro abertis. Si u mundo tá muito paradis? Toma um mé que o mundo vai girarzis! Cevadis im ampola pa arma uma pindureta. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis. Si num tem leite então bota uma pinga aí cumpadi!</p><p class="ng-scope"><br></p><h1 class="ng-scope" id="h1aqui">H1aqui</h1><p class="ng-scope"><br></p><p class="ng-scope">Suco de cevadiss deixa as pessoas mais interessantis. Delegadis gente finis, bibendum egestas augue arcu ut est. Detraxit consequat et quo num tendi nada. Em pé sem cair, deitado sem dormir, sentado sem cochilar e fazendo pose.</p><p class="ng-scope"><br></p><p class="ng-scope"><span><img src="http://192.168.25.112:4444/media/images/_378df1f4c171831e82f815e0508fe6be6791fc37.jpeg" data-uid="_258868d5c512" data-md-img="http://192.168.25.112:4444/media/images/_378df1f4c171831e82f815e0508fe6be6791fc37.jpeg" data-hd-img="http://192.168.25.112:4444/media/images/_2478f370b6dac0e53c9ec2877f85b831b3271fc2.jpeg" width="713" height="401"><span class="image-caption" data-uid="_258868d5c512" style="width: 713px;">Alou sol</span></span>&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;</p><p class="ng-scope">Manduma pindureta quium dia nois paga. Vehicula non. Ut sed ex eros. Vivamus sit amet nibh non tellus tristique interdum. Atirei o pau no gatis, per gatis num morreus. Aenean aliquam molestie leo, vitae iaculis nisl.</p><p class="ng-scope"><br></p><h1 class="ng-scope" id="h1aqui">H1aqui</h1><p class="ng-scope"><br></p><h2 class="ng-scope">H2aqui</h2><p class="ng-scope"><br></p><p class="ng-scope">Nullam volutpat risus nec leo commodo, ut interdum diam laoreet. Sed non consequat odio. Viva Forevis aptent taciti sociosqu ad litora torquent. Quem manda na minha terra sou euzis! In elementis mé pra quem é amistosis quis leo.</p></div>'


console.log(mypage.replace('&#8203;',''))

In this section replace did not work although this string works:

var x = 'AmendoBObo&#8203;';
console.log(x.replace('&#8203;',''))

Then I found out that if I do it this way it will work:

var mypage = '<div pr-compile-html="d.article.body" id="article-body" pr-highlight="" pr-highlight-tooltip="#highlight-tooltip" highlight-class="highlight" class="ng-isolate-scope"><p class="ng-scope">Mussum Ipsum, cacilds vidis litro abertis. Si u mundo tá muito paradis? Toma um mé que o mundo vai girarzis! Cevadis im ampola pa arma uma pindureta. Interessantiss quisso pudia ce receita de bolis, mais bolis eu num gostis. Si num tem leite então bota uma pinga aí cumpadi!</p><p class="ng-scope"><br></p><h1 class="ng-scope" id="h1aqui">H1aqui</h1><p class="ng-scope"><br></p><p class="ng-scope">Suco de cevadiss deixa as pessoas mais interessantis. Delegadis gente finis, bibendum egestas augue arcu ut est. Detraxit consequat et quo num tendi nada. Em pé sem cair, deitado sem dormir, sentado sem cochilar e fazendo pose.</p><p class="ng-scope"><br></p><p class="ng-scope"><span><img src="http://192.168.25.112:4444/media/images/_378df1f4c171831e82f815e0508fe6be6791fc37.jpeg" data-uid="_258868d5c512" data-md-img="http://192.168.25.112:4444/media/images/_378df1f4c171831e82f815e0508fe6be6791fc37.jpeg" data-hd-img="http://192.168.25.112:4444/media/images/_2478f370b6dac0e53c9ec2877f85b831b3271fc2.jpeg" width="713" height="401"><span class="image-caption" data-uid="_258868d5c512" style="width: 713px;">Alou sol</span></span>&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;&#8203;</p><p class="ng-scope">Manduma pindureta quium dia nois paga. Vehicula non. Ut sed ex eros. Vivamus sit amet nibh non tellus tristique interdum. Atirei o pau no gatis, per gatis num morreus. Aenean aliquam molestie leo, vitae iaculis nisl.</p><p class="ng-scope"><br></p><h1 class="ng-scope" id="h1aqui">H1aqui</h1><p class="ng-scope"><br></p><h2 class="ng-scope">H2aqui</h2><p class="ng-scope"><br></p><p class="ng-scope">Nullam volutpat risus nec leo commodo, ut interdum diam laoreet. Sed non consequat odio. Viva Forevis aptent taciti sociosqu ad litora torquent. Quem manda na minha terra sou euzis! In elementis mé pra quem é amistosis quis leo.</p></div>'


console.log(mypage.replace(/&#8203;/g,''))

But I don’t understand what is contained here. Because in a simpler string it worked and in my html it didn’t work?

1 answer

3


Who told you that the first didn’t work?? It did, if you count in your code you have 7 times the sequence &#8203; if you count after you executed without the g it did what expected is to cut/delete the first set of &#8203; found, leaving the others 6. This is what the flag is for g, means global, that is, it will cut from the string all occurrences that give match with the regular expression. Good link to know more about regex.

let x = `X: &#8203;&#8203;&#8203;&#8203;&#8203;`
let y = `Y: &#8203;&#8203;&#8203;&#8203;&#8203;`

console.log(x.replace(/&#8203;/,'') + ' Retirou uma apenas, a primeira encontrada!');

console.log(y.replace(/&#8203;/g,'') + ' Retirou todas!');

  • I thought this replace worked the same way as python, by uploading all occurrences.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.