Home > Blockchain >  How Javascript regex to match start and end tag
How Javascript regex to match start and end tag

Time:04-10

I have a string

<tajweed class=ham_wasl>ٱ foo</tajweed>
<tajweed class=madd>ٱ bar</tajweed>

how to match character inside tag and getting the class value thanks in advance

CodePudding user response:

Don't use regex for this!

Instead, parse it as HTML using a DOMParser, and then use the DOM API to access the data you want.

let htmlString = `
<tajweed class=ham_wasl>ٱ foo</tajweed>
<tajweed class=madd>ٱ bar</tajweed>
`

let domParser = new DOMParser()
let dom = domParser.parseFromString(htmlString, 'text/html')
let tajweeds = dom.querySelectorAll('tajweed')

let firstItemClass = tajweeds[0].className
let secondItemClass = tajweeds[1].className

CodePudding user response:

for your example :

(?<=class=).*?(?=>)

but if you want to select class value between "" then

(?<=>)

demo

  • Related