1
votes

I'm trying to select all characters around some word, but not the word itself with regex in javascript.
(Then I will delete the selected part in JS myself.)

My string is: [id='s_change_b']

I want to select bold and italic part only:

[id=' s_change_b ']

I tried this Regular Expression, but it also selects word inside it.

/\[id='(.*?)'\]/


How can I only select characters around it? I only need Regular Expression part. Thanks in advance!

1
I think you meant using the capturing group on the parts on the outside instead (\[id=')[^\[\]]*('\]) regex101.com/r/1DzGzZ/1The fourth bird
replace(/(\[id=')(.*?)('])/g, '<b>$1</b>$2<b>$3</b>')Wiktor Stribiżew
That will also make it bold instead of selecting it :-)The fourth bird

1 Answers

-1
votes
replace(/(\[id=')(.*?)('])/g, '<b>$1</b>$2<b>$3</b>')