简单的正则表达式替换括号

joh*_*ith 3 javascript regex jquery

有一个简单的方法来制作这个字符串:

(53.5595313, 10.009969899999987)
Run Code Online (Sandbox Code Playgroud)

到这个字符串

[53.5595313, 10.009969899999987]
Run Code Online (Sandbox Code Playgroud)

使用javascript或jquery?

nnn*_*nnn 24

好吧,既然你要求正则表达式:

var input = "(53.5595313, 10.009969899999987)";
var output = input.replace(/^\((.+)\)$/,"[$1]");

// OR to replace all parens, not just one at start and end:
var output = input.replace(/\(/g,"[").replace(/\)/g,"]");
Run Code Online (Sandbox Code Playgroud)

......但那有点复杂.你可以使用.slice():

var output = "[" + input.slice(1,-1) + "]";
Run Code Online (Sandbox Code Playgroud)


Shu*_*ing 7

var s ="(53.5595313, 10.009969899999987)";
s.replace(/\((.*)\)/, "[$1]")
Run Code Online (Sandbox Code Playgroud)


bob*_*bob 7

为了它的价值,替换 ( 和 ) 使用:

str = "(boob)";
str = str.replace(/[\(\)]/g, ""); // yields "boob"
Run Code Online (Sandbox Code Playgroud)

正则表达式字符含义:

[  = start a group of characters to look for
\( = escape the opening parenthesis
\) = escape the closing parenthesis
]  = close the group
g  = global (replace all that are found)
Run Code Online (Sandbox Code Playgroud)