仅在两个子字符串之间搜索和替换字符串

时间:2017-05-23 08:35:46

标签: javascript jquery regex

我有以下字符串(不在DOM中):

var string = '<g>
    <text font-family="Arial" font-size="72" font-weight="bold" style="stroke: rgb(255,255,255); stroke-opacity: 0;   stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" >
        <tspan x="-453.46" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">J</tspan>
        <tspan x="-395.63" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">A</tspan>
        <tspan x="-320.52" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">N</tspan>
    </text>
</g>';

我想删除所有出现的&#34; stroke-opacity:0;&#34;在<tspan></tspan>

因此,如果在"<text" and ">"

之间,则不应该匹配

据我所知,单个正则表达式是不可能的?

3 个答案:

答案 0 :(得分:0)

你需要这个:{$smarty.get.id_order}

&#13;
&#13;
$('text tspan').css('stroke-opacity', '');
&#13;
$('text tspan').css('stroke-opacity', '');
console.dir( $('text').html() )
&#13;
&#13;
&#13;

答案 1 :(得分:0)

如果您要从所有stroke-opacity中删除tspan,而不是从text删除,则可以使用以下行。应该没有理由使用正则表达式

$("tspan").css('stroke-opacity','')

var s = '<g> <text font-family="Arial" font-size="72" font-weight="bold" style="stroke: rgb(255,255,255); stroke-opacity: 0; stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" > <tspan x="-453.46" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">J</tspan> <tspan x="-395.63" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">A</tspan> <tspan x="-320.52" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">N</tspan> </text></g>'

var t = s.split('stroke-opacity:')
var finalStr = "";
for (i=0;i<t.length; i++)
{
  if(t[i].indexOf("<tspan") != -1){
    finalStr += t[i]
  }else {
    finalStr += t[i] + (i != (t.length - 1) ? "stroke-opacity:" : "")
  }
}

$('body').append(finalStr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 2 :(得分:0)

要使用正则表达式执行此操作,可以使用以下正则表达式:

/(<tspan.*?)(stroke-opacity: 0;)/gi

并替换为$1以删除代码中的所有stroke-opacity: 0(如果存在于<tspan>中),如下面的示例所示。

  • (<tspan.*?)是第一个捕获组,它确保stroke-opacity is present within a`
  • (stroke-opacity: 0;)是第二个匹配stroke-opacity: 0无论何时出现
  • 的捕获组
  • /gi代表globalcase-insensitive标志

&#13;
&#13;
var string = '<g>' +
    '<text font-family="Arial" font-size="72" font-weight="bold" style="stroke: rgb(255,255,255); stroke-opacity: 0;   stroke-miterlimit: 10; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" >' +
        '<tspan x="-453.46" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">J</tspan>' +
        '<tspan x="-395.63" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">A</tspan>' +
        '<tspan x="-320.52" y="-7.92" font-size="104" style="stroke: rgb(255,255,255); stroke-opacity: 0;">N</tspan>' +
    '</text>' +
'</g>';
var res = string.replace(/(<tspan.*?)(stroke-opacity: 0;)/gi, "$1");
console.log(res);
&#13;
&#13;
&#13;

相关问题