SophieV
SophieV

Reputation: 13

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.

For example:

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

Upvotes: 0

Views: 60

Answers (2)

Alexander Solonik
Alexander Solonik

Reputation: 10230

Not purely a regex solution but you could do the following , this solution uses the forEach loop and also the match method.

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

Upvotes: 0

PJProudhon
PJProudhon

Reputation: 825

You could use <([^>]*).

  • Will capture any character but > occuring after a <.

Upvotes: 1

Related Questions