Reputation: 853
I have this string:
var value = recordsArray_f006b490[]=1&recordsArray_9715b841[]=1&recordsArray_afb085e2[]=1&recordsArray_fc542e23[]=1&recordsArray_19490084[]=1&recordsArray_615a5055[]=1&recordsArray_32841aa6[]=1&recordsArray_a02a35c7[]=1&recordsArray_d32b5b88[]=1&recordsArray_d32d5339[]=1&recordsArray_56c2b8e10[]=1&recordsArray_8ce9f4211[]=1&recordsArray_5a87afc12[]=1&recordsArray_2b13c0113[]=1&recordsArray_afb085e14[]=1&recordsArray_51db9f216[]=1&recordsArray_2a7470415[]=1&recordsArray_d51057a17[]=1&recordsArray_b274ee918[]=1&recordsArray_d0812dd19[]=1&recordsArray_b1ede8920[]=1&recordsArray_9d76ae821[]=1&recordsArray_0ae779d22[]=1&recordsArray_871777923[]=1&recordsArray_9e22c9f24[]=220236&recordsArray_787c57e25[]=220236&recordsArray_499e25726[]=1&recordsArray_a0600da27[]=220236&recordsArray_9ef1fca28[]=220236&recordsArray_67eee1929[]=220236&recordsArray_24c3a0b30[]=220236&recordsArray_5d2090831[]=220236
And I want to replace "[]=" with "_" and I have the next javascript code:
var regex = new RegExp("[]=", "g");
alert(value.replace(regex, "_"));
but it doesnt change anything, how can I do it? Thanks!
Upvotes: 1
Views: 130
Reputation: 30695
As others have noted, you need to escape the square brackets, as they have special meaning in regex. I want to note, also, that the most clear and terse way to do this is with JavaScript's regex literal notation:
var regex = /\[]=/g;
You also only need to escape the opening [
bracket. Closing brackets (the ]
character) only have a special meaning inside a character class, just as [
only has special meaning outside one.
Upvotes: 2
Reputation: 175748
Escape the first [
to indicate its not defining a character class;
var regex = new RegExp("\\[]=", "g");
Upvotes: 4
Reputation: 263683
escape the brackets as they has special meaning.
var regex = new RegExp("\\[\\]=", "g");
Upvotes: 2
Reputation: 3657
Square brackets are reserved in regex. You need to escape them for matching
var regex = new RegExp("\\[\\]=", "g");
Upvotes: 2