I’m having an issue with matching a square bracket in a string with a regular expression in Javascript. I have tested the regex below, and it works for me:
"step_users[0][step][name]".match(/step_users\[\d*\]/)
This regex matches the substring “step_users[0]”, but what I really need to match is the substring “step_users[0][step]”. I tried modifying the regex as follows, but it fails for me.
/step_users\[\d*\]\[step\]/
In fact, if i even add on the second ‘[‘, it fails. So, this also fails:
"step_users[0][step][name]".match(/step_users\[\d*\]\[/)
Why would it match ‘[‘ for the first square bracket, but fail on the second?
You have a zero-width character in there!
8203 is the word boundary character, a zero-width-space character. In fact, you have it between every pair of braces!
Remove it manually or dynamically as you fetch the data.
How I found it was simple. Your regex should have worked. So, I looked where it stopped working. An easy trick to find these zero-width spaces is to Shift-Arrow along the string.
Edit: A second after submitting, I thought of an easy way to remove it:
This creates a regular expression, which globally searches for the specific ZWS character, and then replaces each occurrence with the empty string.