求一正则表达式:“a'as\'sa's''”=>“a'as\'sa\'s\''”

ywqzxj 2005-04-15 04:27:11
如题,谢谢!
...全文
184 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ywqzxj 2005-04-19
  • 打赏
  • 举报
回复
错了,需要:a'<a>as\'sa\'s\'</a>a'<a>as\'s\'a\'s\'</a>q21'1
你的错误在这一句: str=str.replace(re,"|||");
应该用别的变量存储,比如strx=str.replace(re,"|||");

我更关心或者多更想要更简单高效的方法,利用复杂一点的表达式嵌套不知道怎样?我还没试出来。
ywqzxj 2005-04-18
  • 打赏
  • 举报
回复
你的方法我理解,可是测试不对,我更关心或者多更想要更简单高效的方法,谢谢
gzwuyh 2005-04-18
  • 打赏
  • 举报
回复
不是吧?用上下则转换后就得你需要的结构呀,怎么不对呢?
输入字符:
a'<a>as\'sa's'</a>a'<a>as\'s'a's'</a>q21'1
转换结果:
a'<a>as\'sa\'s\'</a>a'<a>as\'sa\'s\'</a>q21'1
这不是你需要的?
gzwuyh 2005-04-17
  • 打赏
  • 举报
回复
再看看呢?
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>原字符替换字符</title>
</head>

<body>
<script language=javascript>

function repl_test()
{
var str=f1.t1.value;
var re=/(<a>[a-zA-Zs\\\d']*<\/a>)/g;
var arr=re.exec(str);
if(arr!=null)
{
str=str.replace(re,"|||");
for(var i=0;i<arr.length;i++)
{
alert(arr[i]);
alert(/[^\\]'/g.test(arr[i]));
arr[i]=arr[i].replace(/\\'/g,"'");
arr[i]=arr[i].replace(/'/g,"\\'");
str=str.replace("|||",arr[i]);
}
}

f1.t2.value=str;

}
</script>

<form name=f1>原字符<input name=t1>替换字符<input name=t2>
<input value="替换" type=button onclick="repl_test()">

</form>
</body>

</html>
ywqzxj 2005-04-17
  • 打赏
  • 举报
回复
谢谢,对于一下情况不能正确分析,另难道没有更简单高效的方法吗?
a'<a>as\'sa's'</a>a'<a>as\'s'a's'</a>q21'1
ywqzxj 2005-04-16
  • 打赏
  • 举报
回复
就是找出串中<a>与</a>之间“\'”或“'”,替换成“\'”。
gzwuyh 2005-04-16
  • 打赏
  • 举报
回复
看看是否符合要求?
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>原字符替换字符</title>
</head>

<body>
<script language=javascript>

function repl_test()
{
var str=f1.t1.value;
var re=/<a>\S*<\/a>/g;
var arr=re.exec(str);
if(arr!=null)
{
str=str.replace(re,"|||");
for(var i=0;i<arr.length;i++)
{
alert(arr[i]);
alert(/[^\\]'/g.test(arr[i]));
arr[i]=arr[i].replace(/\\'/g,"'");
arr[i]=arr[i].replace(/'/g,"\\'");
str=str.replace("|||",arr[i]);
}
}

f1.t2.value=str;

}
</script>

<form name=f1>原字符<input name=t1>替换字符<input name=t2>
<input value="替换" type=button onclick="repl_test()">

</form>
</body>

</html>
cj217 2005-04-16
  • 打赏
  • 举报
回复
什么意思,不明白.
faisun 2005-04-15
  • 打赏
  • 举报
回复
看不懂的..
为什么有的 ' 前要加 \ 为什么有的又不要啊,什么情况下要加?

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧