how can i get the index of matching pattern in the string using javascript?
consider string original_string = "i am [1@some user] some more text [2@another user]"
i am using pattern /[\d+@(?[^]\r\n]*)]/g to match strings in square brackets
then i use string.matchAll(original_string) to get the matches
const matches = string.matchAll(original_string);
let names = [];
for (const match in matches) {
names.push(match);
}
now the names array will contain ["some user", "another user"]
now how do i get the index of the first match in original string from names array.
const final_string = []
const original_string = "i am [12@some user] some text [2@some user2]"
let prev_match_pos = 0
for each match in original_string
final_string.push(text from the end of prev match up until current
match start)
final_string.push(<span>match</span>)
update prev_match_pos
final_string.push(text from the end of previous match to the end of the
original string)
I want to implement the above algorithm in javascript.
Basically i want to convert this string "i am [1@some user] some more text [2@another user]"
to
"i am <span>some user</span> some more text `another user'"
how can i do it?
the basic implementation is as below,
get the strings in brackets. from the string in brackets extract value after @ character. then embed the extracted value in span tag and place them in the original string.
could someone help me with this. thanks.