RegExp problem, remove iframe

Post Reply
guidoolijslager
Newbie
Posts: 2
Joined: Tue May 19, 2020 9:58 am

RegExp problem, remove iframe

Post by guidoolijslager » Wed May 20, 2020 10:38 am

Hello,

I have a problem to replace tekst in a string with a regular expression.
The regular expression works on https://regex101.com (see enclosed picture). I found it on the Internet.
I can't get it to work in my script, it does nothing.
I tried many variations.

dummyTekst = dummyTekst.replace(/(?:<iframe[^>]*)(?:(?:\/>)|(?:>.*?<\/iframe>))/g, "");

Below the tekst to be cleaned example:

En het gaat steeds sneller. In de periode van 1996 tot 2015 is het areaal ruim 5% kleiner geworden, blijkt uit cijfers van het CBS. <iframe class=localfocusvisual" frameborder="0" style="width:100%;height:400px;overflow:hidden" src="https://localfocus2.appspot.com/5e0dc97bddf92"></iframe> Ruim de helft van die 125.000 ha voor de landbouw verloren hectares kreeg woningbouw als nieuwe bestemming. <iframe class="localfocusvisual" frameborder="0" style="width:100%;height:400px;overflow:hidden" src="https://localfocus2.appspot.com/5e0dc996cea71"></iframe> Iets meer dan de helft van de landbouwgrond in 2018 was grasland.

What is wrong with my script? The iframe tags with the text inside has to be removed.

Thanks in advance,

regards,
Guido
Attachments
Schermafbeelding 2020-05-20 om 10.31.58.png
Schermafbeelding 2020-05-20 om 10.31.58.png (93.67 KiB) Viewed 220 times

Padawan
Advanced member
Posts: 252
Joined: Mon Jun 12, 2017 8:48 pm

Re: RegExp problem, remove iframe

Post by Padawan » Wed May 20, 2020 3:14 pm

Personally, I would have preferred to use this:
dummyTekst = dummyTekst.replace(/<iframe.+?><\/iframe>/g, "");

But unfortunately, Switch Regular expressions don't support the lazy quantifier (the "+?"), so it doesn't work. You can see what Switch regex supports in the documentation:
https://www.enfocus.com/manuals/UserGui ... sions.html

I have a very ugly solution which works in your test:
dummyTekst = dummyTekst.replace(/<iframe.{3,180}><\/iframe> ?/g, "");

Basically, I assume that the amount of text will always be between 3 and 180 characters. If you can be sure that the text of the iframe will always be more or less the same length, then this can be a workable solution.

guidoolijslager
Newbie
Posts: 2
Joined: Tue May 19, 2020 9:58 am

Re: RegExp problem, remove iframe

Post by guidoolijslager » Fri May 22, 2020 4:05 pm

Hi Padawan,

thanks for your reply.
The solution worked for me. :)

Do you also have an solution to quick test the regular expression from Switch?

Padawan
Advanced member
Posts: 252
Joined: Mon Jun 12, 2017 8:48 pm

Re: RegExp problem, remove iframe

Post by Padawan » Fri May 22, 2020 8:09 pm

I used a script to test it, I think in your use case this is the only option.

Post Reply