简体   繁体   English

正则表达式不仅可以在两个特定符号之间匹配字符,还可以在单​​个符号之后匹配字符

[英]Regex to match characters not only between two specific symbols but also after a single one

I'm using this regex: 我正在使用此正则表达式:

/<([^>]*)>/g

for calculating the number of characters between < and > symbols using JavaScript, but I also need to count the number of characters added after a < but before closing the successive > when a user is typing. 使用JavaScript计算<>符号之间的字符数,但是当用户键入时,我还需要计算<但关闭连续的>之前添加的字符数。

For example: 例如:

<I need to count this> this not <this yes> and <also this

You could use <([^>]*) . 您可以使用<([^>] *)

  • Will capture any character but > occuring after a < . 将捕获除<之后出现的>任何字符。

Not purely a regex solution but you could do the following , this solution uses the forEach loop and also the match method. 不只是正则表达式解决方案,而是您可以执行以下操作,此解决方案使用forEach循环以及match方法。

var newArr = [];
str.match(/<([^>]+)/g).forEach( (e , i) => newArr.push( e.slice(1) ) ); // ["I need to count this", "this yes", "also this"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM