Javascript 在Jquery中使用Match只返回一个值
我试图使用“Match”来过滤文本区域中的一些值,但我的代码只适用于第一行,只返回“one.png;1” 我需要获取“text2”div中的所有值,比如:“one.png;1/two.png;2/three.png;3”Javascript 在Jquery中使用Match只返回一个值,javascript,jquery,regex,match,Javascript,Jquery,Regex,Match,我试图使用“Match”来过滤文本区域中的一些值,但我的代码只适用于第一行,只返回“one.png;1” 我需要获取“text2”div中的所有值,比如:“one.png;1/two.png;2/three.png;3” $(文档).ready(函数(){ var dataimg=$('#text').text().match('url_image=“(.*)”); var datasub=$('#text').text().match('sub=(.*)); $(“#text2”).appe
$(文档).ready(函数(){
var dataimg=$('#text').text().match('url_image=“(.*)”);
var datasub=$('#text').text().match('sub=(.*));
$(“#text2”).append(dataimg[1]).append(“;”).append(datasub[1]);
});代码>
url_image=“one.png”;sub=1;url_image=“two.png”;sub=2;url_image=“three.png”;sub=3;
如果您只是尝试更新文本格式,则可以使用全局替换,例如:
/url_image="([^"]+)"; sub=(.*);/g
并将匹配项替换为“”$1$2'
:
$('#text2').text(函数(){
返回$('#text').text().replace(/url#u image=“([^“]+)”;sub=(.*);/g,'$1;$2');
});
url_image=“one.png”;sub=1;
url_image=“two.png”;sub=2;
url_image=“three.png”;sub=3;