我有一些带有javascript函数的html文件,比如这个
<!-- some content -->
<div conmousedown="My_Function('FirstParamterThatCanBeAnything', '')">
<!-- some content -->
</div>
<!-- some content -->
我希望能够使用替换(http://msdn.microsoft.com/en-us/library/ewy2t5e0(v=vs.110).aspx) 设置第二个参数而不更改其余参数:
<!-- some content -->
<div conmousedown="My_Function('FirstParamterThatCanBeAnything', 'SOMENEWVALUE')">
<!-- some content -->
</div>
<!-- some content -->
第一个参数是一个带有随机参数的网址,它永远不会是相同的
有人可以帮助我找到正则表达式吗?
答案 0 :(得分:1)
从评论中,这是我建议的正则表达式:
(My_Function\((?:[^,']+|("|')(?:(?!\2).)*\2), ')('\))
故障:
( # Open 1st Capture group
My_Function\( # Match My_Function(
(?:
[^,']+ # Match any non comma/quote characters (for numeric param)
| # Or
("|') # A quote stored in 2nd Capture group
(?:(?!\2).)* # Any character except the quote that matched
\2 # The quote that matched
)
, ' # Match a comma, a space and a single quote
) # End 1st Capture group
( # Open 3rd Capture group
'\) # Match single quote and )
) # Close 2nd Capture group
在C#中实现它将有点像这样:
Regex regex = new Regex(@"(My_Function\((?:[^,']+|(""|')(?:(?!\2).)*\2), ')('\))");
str = regex.Replace(text, "$1SOMENEWVALUE$3");
text
包含页面的位置。