[英]How to replace first character only for matched regex?
regOpenTags = new RegExp("<+[a-zA-Z]", "g");
matches html opening tags (eg <body>
) without matching closing tags (eg </body>
). 匹配html开始标记(例如
<body>
)而没有匹配的结束标记(例如</body>
)。
I'd like to replace the first character of matched tags which is <
, but doing 我想替换匹配标签的第一个字符
<
,但正在做
parsedString = string.replace(regOpenTags, '');
would replace also the first letter. 也将取代第一个字母。 In other words, when using
replace()
on a string containing <body>
, I'd prefer to get body>
instead of ody>
. 换句话说,当在包含
<body>
的字符串上使用replace()
时,我更喜欢使用body>
而不是ody>
。
How can I use the replace()
method or regex to achieve this? 我如何使用
replace()
方法或正则表达式来实现这一目标?
You can use a Positive Lookahead to achieve this. 您可以使用Positive Lookahead来实现此目的。
regOpenTags = new RegExp("<(?=[a-z])", "gi");
Regular expression 正则表达式
< '<'
(?= look ahead to see if there is:
[a-z] any character of: 'a' to 'z'
) end of look-ahead
Another way of using a Positive Lookahead with negation . 使用带有否定的正向前瞻的另一种方式。
regOpenTags = new RegExp("<(?=[^/])", "g");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.