简体   繁体   中英

How to replace two consecutive special characters in string using regex?

I need to replace the "[ by [ and ]" by ] combinations everytime found in this string.

I tried this but it doesn't work

var myString = myString .replace(/]"/i, /]/);
  • need to use single quotes on the outside so that myString is actually a single string
  • need a backslash before ] in regex
  • do the second argument of replace as a string, not a regex probably also should use the g flag for global
  • so myVar = myVar.replace( /\\]"/g, "]").replace( /"\\[/g, "[")

I can assume that you want to convert/format some kind of string to an object, You might take advantage of JSON.parse() function. See documentation here

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM