Is there a way to retrieve the (starting) character positions inside a string of the results of a regex match() in Javascript?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
exec
returns an object with aindex
property:And for multiple matches:
Here is a cool feature I discovered recently, I tried this on the console and it seems to work:
Which returned: "border 6 bottom 13 left 18 radius"
So this seems to be what you are looking for.
You can use the
search
method of theString
object. This will only work for the first match, but will otherwise do what you describe. For example:Here's what I came up with:
This member fn returns an array of 0-based positions, if any, of the input word inside the String object
Now try
You can also input regular expressions:
Here one gets the position index of linear term.