I a using the current regex /(?:^|[^\/\\])\[([\w\s-]*)($|\[)/
to match when user starts to enter chars and enter [some tex, to match this. When he closes it stops the match.
The problem is that i added condition in front, the ?:
and if he enter \[
or /[
, i am not going to match it.
Well but, if he has something like [text][bla
- in this i will match the previous ]
bracket and the string will be ][bla
, but i don't want this. If he enters [test] [car
- this will match the space in front and will be _[car
- _
is space.
I want from both to match only from opening bracket to, not before it. Do you guys have some ideas. Thank you!
I am not totally clear, but if I understand you correctly, you would like to detect pairs of [
and ]
angle brackets, but skip over /
and \
escaped angle brackets like \]
and /]
, correct?
So [aaa]
would be one pair, [bbb/[ccc/]ddd]
another, and [eee\[fff\]ggg]
another.
Here is how you can parse that with three regular expressions:
var str = 'Test [1st], and [2nd\\[with\\]escapes], and [3nd/[more/]escapes][4thNoSpace] text'; var re = /(\[[^\]]*\])/g; var result = str.replace(/([\\\/])\]/g, '$1\x01') // replace escapes with tokens.replace(re, function(m, p1) { // process bracket pairs return '<b>' + p1 + '</b>'; }).replace(/(.)\x01/g, '$1\]'); // restore escapes from tokens console.log('before: ' + str); console.log('result: ' + result); document.body.insertAdjacentHTML('beforeend', result);
So, this input:
Test [1st], and [2nd\[with\]escapes], and [3nd/[more/]escapes][4thNoSpace] text
...produces this output:
Test <b>[1st]</b>, and <b>[2nd\[with\]escapes]</b>, and <b>[3nd/[more/]escapes]</b><b>[4thNoSpace]</b> text
Explanation:
In the future it would be helpful to specify example input strings, and expected output for each input.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.