正则表达式:如何在两个引号之间找到逗号
Regex: how to find a comma between two quotation marks
我需要找到两个引号之间的点,并将其替换为逗号。
我正在尝试这个
\".*?\"
但它会找到引号之间的所有内容。
我需要将类似“100,21$”的内容转换为“100.21$”
一般来说,您可以先 匹配 带引号的子字符串
"[^"]+"
然后在匹配块中用 ,
替换 .
(在回调中,或 post-process method/function)。
或者,您可以使用捕获 + 反向引用:
"(\d*),(\d+$)"
替换为"."
(其中</code>是用<code>(\d*)
捕获的文本,</code>是用<code>(\d+$)
捕获的值)。参见 this demo。
如果你的字符串像你的例子一样简单:"100,21$"
您可以简单地使用此代码:
str = str.replace(/,/g,'.');
如果有点复杂,比如:
Here is an "example" of "100,21$" price, I am a string
你可以使用这个:
str = str.replace(/"(\d+),(\d+)(.*?)"/g,'".")
Regexr link 如果你想测试:
http://regexr.com/3dbo5
nb : 我写了例子,但你可以在其他语言中使用正则表达式和字符串模式
我需要找到两个引号之间的点,并将其替换为逗号。
我正在尝试这个
\".*?\"
但它会找到引号之间的所有内容。
我需要将类似“100,21$”的内容转换为“100.21$”
一般来说,您可以先 匹配 带引号的子字符串
"[^"]+"
然后在匹配块中用 ,
替换 .
(在回调中,或 post-process method/function)。
或者,您可以使用捕获 + 反向引用:
"(\d*),(\d+$)"
替换为"."
(其中</code>是用<code>(\d*)
捕获的文本,</code>是用<code>(\d+$)
捕获的值)。参见 this demo。
如果你的字符串像你的例子一样简单:"100,21$"
您可以简单地使用此代码:
str = str.replace(/,/g,'.');
如果有点复杂,比如:
Here is an "example" of "100,21$" price, I am a string
你可以使用这个:
str = str.replace(/"(\d+),(\d+)(.*?)"/g,'".")
Regexr link 如果你想测试: http://regexr.com/3dbo5
nb : 我写了例子,但你可以在其他语言中使用正则表达式和字符串模式