帮忙写个正则表达式

luohuayh 2009-11-16 10:44:19
如下:
String str="要提取的内容";
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ '</div>';
现在我要写一个正则表达式要取出<div></div>之间的字符请问该怎么写,拜托各位大虾了!!!
...全文
170 24 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
luohuayh 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 george3057 的回复:]
Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
String  a = "a";

Pattern p2 = Pattern.compile(a);
Matcher m2 = p2.matcher(str);
String str2 = m2.replaceAll(" <strong>"+a+" </strong>");

Pattern p3 = Pattern.compile(str);
Matcher m3 = p3.matcher(目标串);
m3.find();
StringBuffer sb = new StringBuffer();
m3.appendReplacement(sb, str2);
m3.appendTail(sb);

System.out.println(sb.toString());


[/Quote]
你用的都是java语言,怎么用JS方法实现呢,java语言我不太明白!
george3057 2009-11-16
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("^(<div[.*[^/]]*>)(.*)(</div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
String a = "a";

Pattern p2 = Pattern.compile(a);
Matcher m2 = p2.matcher(str);
String str2 = m2.replaceAll("<strong>"+a+"</strong>");

Pattern p3 = Pattern.compile(str);
Matcher m3 = p3.matcher(目标串);
m3.find();
StringBuffer sb = new StringBuffer();
m3.appendReplacement(sb, str2);
m3.appendTail(sb);

System.out.println(sb.toString());

luohuayh 2009-11-16
  • 打赏
  • 举报
回复
给帖子加了20分,希望哪位大虾帮帮忙!
luohuayh 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 luohuayh 的回复:]
引用 14 楼 george3057 的回复:
Matcher对象的replaceAll()方法 替换掉 所有 能匹配上的
你这里的 就是"a"  把所有 "a"都换成 <strong>a </strong>

9楼的那个 测试成功。 

JS只知皮毛。。 

 

测试所得出的结果不是我想要的结果,你的方法最后是把整句替换了,而我要的不是把整句替换,只是要把 <div> </div>里的a替换,如
var a="a";
var str="abcdefg";
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ '  </div>';

匹配和替换后想要得到的最后结果是
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' <strong>a </strong>bcdefg' </div>';
[/Quote]
而13楼楼主的方法测试后所得到的结果是这样的
<strong>a </strong;

luohuayh 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 george3057 的回复:]
Matcher对象的replaceAll()方法 替换掉 所有 能匹配上的
你这里的 就是"a"  把所有 "a"都换成 <strong>a </strong>

9楼的那个 测试成功。 

JS只知皮毛。。 

 
[/Quote]
测试所得出的结果不是我想要的结果,你的方法最后是把整句替换了,而我要的不是把整句替换,只是要把<div></div>里的a替换,如
var a="a";
var str="abcdefg";
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ ' </div>';

匹配和替换后想要得到的最后结果是
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' <strong>a </strong>bcdefg' </div>';
george3057 2009-11-16
  • 打赏
  • 举报
回复
Matcher对象的replaceAll()方法 替换掉 所有 能匹配上的
你这里的 就是"a" 把所有 "a"都换成 <strong>a</strong>

9楼的那个 测试成功。

JS只知皮毛。。

closewbq 2009-11-16
  • 打赏
  • 举报
回复

public class Test {
public static void main(String[] args){
String A="a";
String str="abcdefg";
String str1="<div>"+str+"</div>;";
Pattern p=Pattern.compile("<div[^>]*>(.[^>]*)</div>");
Matcher m=p.matcher(str1);
if(m.find())
System.out.println(str1.replace(m.group(1), m.group(1).replaceAll(A,"<strong>a</strong>")));
}
}

luohuayh 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 closewbq 的回复:]
引用 8 楼 luohuayh 的回复:
引用 6 楼 george3057 的回复:
Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);

不好意思,请问怎么把匹配到的内容换成' <strong>'+A+' </strong>'呢,小弟我对正则表达式不是很了解,拜托了!

Java code
str是你以前的<div>。。。的字符串
str=str.replaceAll("</?div[^>]*>","");
String newStr=str.replace(str,"<strong>'"+A+"'</strong>");
[/Quote]
replaceAll方法是不是把整句str都替换了?但我要的结果不是把整句替换,只是把找到的结果替换掉,也就是说只替换<div></div>中匹配到的那单个字符。怎么说好呢,我举个例子吧,
var a="a";
var str="abcdefg";
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ ' </div>';

匹配和替换后得到的最后结果是
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' <strong>a</strong>bcdefg'</div>';
closewbq 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 luohuayh 的回复:]
引用 6 楼 george3057 的回复:
Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);

不好意思,请问怎么把匹配到的内容换成' <strong>'+A+' </strong>'呢,小弟我对正则表达式不是很了解,拜托了!
[/Quote]

str是你以前的<div>。。。的字符串
str=str.replaceAll("</?div[^>]*>","");
String newStr=str.replace(str,"<strong>'"+A+"'</strong>");
luohuayh 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 george3057 的回复:]
引用 5 楼 luohuayh 的回复:

var A="a";
var str="asdasda"
  <div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ '  </div>';
现在我要写一个正则表达式要取出  <div>  </div>之间和变量a相匹配的字符,并把该字符替换成 <Strong>A </strong>请问该怎么写,拜托各位大虾了!!!

Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
String  a = "........";

Pattern p2 = Pattern.compile(a);
matcher m2 = p2.matcher(str);
System.out.println(m2.replaceAll(" <strong>"+a+" </strong>"));


[/Quote]
能不能换成JAVASCRIPT语言,我用的是javascript
george3057 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 luohuayh 的回复:]

var A="a";
var str="asdasda"
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ ' </div>';
现在我要写一个正则表达式要取出 <div> </div>之间和变量a相匹配的字符,并把该字符替换成 <Strong>A </strong>请问该怎么写,拜托各位大虾了!!!
[/Quote]
Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
String a = "........";

Pattern p2 = Pattern.compile(a);
matcher m2 = p2.matcher(str);
System.out.println(m2.replaceAll("<strong>"+a+"</strong>"));

luohuayh 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 george3057 的回复:]
Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
[/Quote]
不好意思,请问怎么把匹配到的内容换成'<strong>'+A+'</strong>'呢,小弟我对正则表达式不是很了解,拜托了!
closewbq 2009-11-16
  • 打赏
  • 举报
回复

str.replaceAll("</?div[^>]*>","");
george3057 2009-11-16
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("^( <div[.*[^/]]*>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
luohuayh 2009-11-16
  • 打赏
  • 举报
回复

var A="a";
var str="asdasda"
<div class="out" onclick="' +this.name+'.bbb(this)" onmouseover="' +this.name+'.ccc(this,\'over\')" onmouseout="' +this.name+'.dddd(this,\'out\')">' +str+ ' </div>';
现在我要写一个正则表达式要取出 <div> </div>之间和变量a相匹配的字符,并把该字符替换成<Strong>A</strong>请问该怎么写,拜托各位大虾了!!!
george3057 2009-11-16
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("^( <div.*[^/]?>)(.*)( </div>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
Landor2004 2009-11-16
  • 打赏
  • 举报
回复
var str="<div class=\"out\" ...>....xxxx1111xxx3#$%</div>"
var reg = /<div .*>([^\</]*)<\/div>/g
alert(reg.test(str))
alert(RegExp.$1)
george3057 2009-11-16
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("^(<.+[^/]>)(.*)(</.+>)$");
Matcher m = p.matcher(目标串);
m.find();
String str = m.group(2);
sunnylyy 2009-11-16
  • 打赏
  • 举报
回复
String div = "<div class=\"out\" onclick=\"\">abcde</div>";
Pattern p = Pattern.compile("(<div .*?>)(.*)(</div>)");
Matcher m = p.matcher(div);
if(m.find()){
System.out.println(m.group(2));
}


输出abcde
luohuayh 2009-11-16
  • 打赏
  • 举报
回复
我新开个贴,你们帮我看看。
http://topic.csdn.net/u/20091116/13/00c6cfc8-28ce-4d7d-98cf-0fbb2f3dae39.html
加载更多回复(4)

87,997

社区成员

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

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