简体   繁体   English

jQuery从标签中删除括号/文本

[英]jQuery Remove Brackets/Text from Label

Here is a snippet of HTML that loads on the site, I cannot change this as it's loaded via code blocks in 3D Cart: 这是网站上加载的HTML片段,由于它是通过3D购物车中的代码块加载的,因此我无法更改它:

    <div class="opt-field">
                <label for="text56">4kg - $19 [+$19.00]</label>
                <input type="text" size="3" name="text56" value="0"><br>

                <label for="text58">8kg/18lb - $29 - Currently Out of Stock [+$29.00]</label>
                <input type="text" size="3" name="text58" value="0"><br>

                <label for="text59">12kg/26lb - $39 - Currently Out of Stock [+$39.00]</label>
                <input type="text" size="3" name="text59" value="0"><br>
</div>

And I want to remove the automatically generated [+$price] that is attached to each label. 我想删除附加到每个标签的自动生成的[+ $ price]。 I have tried many variations I have found on here but nothing seems to affect it. 我尝试了许多在这里找到的变体,但似乎没有影响。

    $(document).ready(function() {
        var str = $('.opt-field label');
        var r = str.replace(/[(\[].*?[)\]] */g, "");
        $('label').html(r);
    });

Here is the jsfiddle I have been trying it on: https://jsfiddle.net/32rch0n9/1/ 这是我一直在尝试的jsfiddle: https ://jsfiddle.net/32rch0n9/1/

Thanks for any help/suggestions 感谢您的帮助/建议

If you want to just remove the brackets you can use the following regex 如果只想删除括号,则可以使用以下正则表达式

[\*?[)\]]

If you want to remove the brackets and price inside use the following regex 如果要删除括号和内部价格,请使用以下正则表达式

[\[].*?[)\]]

Also, you can use regexr.com for testing 另外,您可以使用regexr.com进行测试

http://regexr.com/3dd0l http://regexr.com/3dd0l

Reference: [https://jsfiddle.net/32rch0n9/5/][1]

Your regular expression works. The catch is you have to apply for each label. Refer to the above fiddle for sample.


  [1]: https://jsfiddle.net/32rch0n9/5/

Your mistake is your JS code, you are not working with the list of labels. 您的错误是您的JS代码,您不使用标签列表。

$('.opt-field label').each(function(index, item){
        console.log($(item).text());
        var r = $(item).text().replace(/[(\[].*?[)\]] */g, "");
        $(item).html(r);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM