Home > OS >  taking out html from a string with regex
taking out html from a string with regex

Time:09-23

Hi,

I have a string that looks like this:

<span class='$variable'>

I want to strip everything else but the $variable inside so I tried this:

string.replace('/<span class=\'/','/\'>/');

I have used this regex on php but it doesnt seem to work on javascript. What is the correct regex in this case?

Thank you.

CodePudding user response:

Use

.replace(/<span class='(.*?)'>/, '$1')

See regex proof.

EXPLANATION

PATTERN MEANING
<span class=' <span class='
( group and capture to $1:
.*? any character except \n (0 or more times (matching the least amount possible))
) end of $1
'> '>
  • Related