简体   繁体   中英

How do I remove text in BETWEEN patterns in regex javascript?

I have some HTML tags that I need to parse out for saving to database.

<div class="hello" id="one"> </div> 
<span id="123123" > </span>

All I want remaining is

<div> </div>
<span> </span>

How can i do this with regex?

I know that I can do it in HTML-- but the point isn't to re-display it without classes, but to save it to a database bare-bones.

ALso hoping to avoid lookbacks

Thanks

Use DOMParser instead, and iterate over the attributes of each element, and remove each attribute:

 const htmlText = `<div className="hello" id="one"> </div> <span id="123123" > </span>`; const doc = new DOMParser().parseFromString(htmlText, 'text/html'); doc.querySelectorAll('*').forEach((elm) => { [...elm.attributes].forEach(({ name }) => { elm.removeAttribute(name); }); }); console.log(doc.body.innerHTML); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM