使用正则表达式从字符串中取出 html

taking out html from a string with regex

嗨,

我有一个看起来像这样的字符串:

<span class='$variable'>

我想去除除 $variable 之外的所有内容,所以我尝试了这个:

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

我在 php 上使用了这个正则表达式,但它似乎在 javascript 上不起作用。在这种情况下正确的正则表达式是什么?

谢谢。

使用

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

regex proof

解释

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