如何选择//之后和同一行中的所有内容


How to select everything after // and in the same line?

我有一个字符串,这个字符串是:

$str = 'this is a // comment
        I want to select this // comment';

现在我想选择在它后面和单行中有//的所有内容。然后把它放在<span class="comment-method"></span>之间。在这种情况下,我想要这个输出:

$newstr = 'this is a <span class="comment-method"> comment </span>
           I want to select this <span class="comment-method"> comment </span>';

我该怎么做?

/'/'/(.*)/g

https://regex101.com/r/aU6rD4/2

这将捕获//之后并且在同一行上的所有内容。

在php:中

$str = 'this is a // comment
I want to select this // comment';
$newstr = preg_replace('/'/'/(.*)/', "<span class='"comment-method'">$1</span>", $str);

https://3v4l.org/CC4fm

/'/'/'s*(.*)$/gm

m表示将$视为行的末尾,而不是字符串的末尾。g表示全局,查找所有匹配