假设有以下字符串:
这让我得到了其中的字符串,但我在进行交换时遇到了困难。
b*any string here*
如果存在这种情况,我想把开头的b*替换为<b>
,并把结尾的*替换为</b>
(请忽略反斜杠,我在SO网站上需要它来转义)。
此外,可能会有多个匹配:
b*any string here* and after that b*string b*.
不应处理这些情况:
b*foo bar
foo bar*
bb*foo bar* (b is not after a whitespace or beginning of string).
我已经做到了这一步:
(?<=b\*)(.*?)(?=\*)
这让我得到了其中的字符串,但我在进行交换时遇到了困难。
\
your html entities/code here``以将其显示为代码。 - David Thomas/\bb\*(.*?)\*/g
,并用<b>$1</b>
替换。或者,如果你真的想要一个空格边界,可以使用.replace(/(^|\s)b\*(.*?)\*/g, "$1<b>$2</b>")
。 - Wiktor Stribiżew