KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
If I use string.match() with a regex, I'll get back the matched string, but not the index into the original string where the match occurs. If I do string.search(), I get the index, but I don't necessarily know how long the matched part of the string is. Is there a way to do both, so I can get the index of the end of the match in the original string? I suppose I could do one after the other (below), assuming they return the same results but in a different way, but that seems ugly and inefficient, and I suspect there is a better way. var str = "Fear leads to anger. Anger leads to hate. Hate leads to suffering"; var rgx = /l[aeiou]+d/i; var match = str.match(rgx); if (match && match[0]) { var i = str.search(rgx); console.log ("end of match is at index " + (i+match[0].length)); }
Tags (comma-separated)
Save Edits
Cancel