【问题标题】:AS3 / AIR 3.5 Replace only part of a stringAS3 / AIR 3.5 仅替换字符串的一部分
【发布时间】:2013-02-11 06:15:12
【问题描述】:

AS3 |闪光 CS6 | Adobe Air 3.5

我有一个很长的字符串,最终会被格式化为 XML。在字符串中,我有几个类似以下内容的实例:

<title>Some title</title>

//the following line would need changed
<title>Some title</garbage>

<title>Some title2</title>
<title>Some title3</title>

//the following line would need changed
<title>Someone's title & (his info)</garbage>

//the following line should NOT change
<another title>something else</garbage>

我需要搜索并替换所有这些:

<title>(whatever is here)</garbage>

然后制作它们:

<title>(whatever is here)</title>

如您所见,我不能简单地搜索和替换“垃圾”,因为它不适用于每个实例...我只想替换那些使用

...而不是 <another title> 另请注意,不确定 <title> 和 之间的字符是什么 <p>因此,尽管我知道这行不通……但我的想法是:</p> <pre><code>str = str.replace(/<title>\w+<\/garbage>/, "<title>\w+<\/title>"); </code></pre> <p>我认为答案在此页面上的“捕获子字符串”下,但到目前为止还没有运气: <a href="/default/index/tourl?u=aHR0cDovL3d3dy5zZXZlbnNvbi5jb20uYXUvYWN0aW9uc2NyaXB0L3Rlc3RpbmctcmVndWxhci1leHByZXNzaW9ucy8%3D" rel="nofollow" target="_blank">http://www.sevenson.com.au/actionscript/testing-regular-expressions/</a></p> <p>此时我似乎只是在随机插入一些东西,所以寻求帮助更有意义。</p> <p>感谢任何建议。</p>

【问题讨论】:

    标签: actionscript-3 flash air flash-cs5


    【解决方案1】:

    匹配&lt;title&gt;之后和&lt;/garbage&gt;之前的任何符号

    str = str.replace(/(<title>.[^<]+)<\/garbage>/, "$1</title>");
    

    例如http://regexr.com?33nog

    【讨论】:

    • 我发现了一个问题。如果 (whatever is here) 只有一个字符长,则此表达式不起作用。关于如何解决这个问题的任何建议?对于 2 个或更多字符,这很棒。
    • 编辑:让它非贪婪修复它。 /(.[^/, "$1" 变成 /(.?[^/, "$1"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 2022-07-18
    • 1970-01-01
    • 2011-01-06
    • 2012-05-18
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多