K
K
kevus2018-06-14 15:44:36
JavaScript
kevus, 2018-06-14 15:44:36

What regular expression (JS) is suitable for removing links?

Hello. Please tell me the code that will remove all href="" from the page.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2018-06-14
@kevus

You don't need regular expressions for this.
And in general, regular expressions are not particularly suitable for parsing HTML, since they are sharpened to work with regular grammars , while HTML is context-free and NOT a regular language.
If you are working with a ready DOM tree on the page, it will be much more efficient to process it not as text, but to use the functions built into js.

document.querySelectorAll('a[href=""]') // найти все элементы a с атрибутом href
    .forEach(el => el.removeAttribute('href')) // перебрать элементы и удалить этот атрибут

N
nonamich, 2018-06-14
@dimaeromin

https://regexr.com/3r237 right?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question