我拿到了大梁:
(a+)(,)(b+),它正在分析字符串:aaaa,bbb
保持$1 , $2, $3.组对我来说很重要
我需要一个Regex用法(只使用,没有Js,c#等),它会给我这样的印象:
kkkk,ppp流:
1) emit group1 , group2 , group3
2) keep the $2 group
3)replace the 'a' to 'k' ( as many as 'a'.count)
3)replace the 'b' to 'p' ( as many as 'b'.count)编辑
我对Js中的string.replace(regexp/substr,newstring)没有问题
但我不想要任何循环之类的东西。
发布于 2011-12-06 11:38:22
试试这个:
str.replace(
new RegExp("([ab]+)", "g"),
// Here, we are passing in a nameless function as the parameter for
// the "replace" argument of the String Replace method. It uses the $1
// to refer to the grouping of a or b.
function($1){
if ($1.charAt(0) == "a"){
// replace with k
return( $1.replace(/./g, "k") );
} else {
// replace with p
return( $1.replace(/./g, "p") );
}
}
)根据你自己的需要调整它。
发布于 2011-12-06 10:34:06
正则表达式是一种模式匹配机制。您所要求的是模式替换。这是可能的额外的语言结构,不同的语言,但不是通过正则表达式本身。由于你明确排除了任何语言细节,所以你的问题没有答案。
编辑:
好的,现在您已经编辑了您的问题,并允许语言特定的解决方案。见艾德的回答。
https://stackoverflow.com/questions/8398348
复制相似问题