简体   繁体   中英

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:

    <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. 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/

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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