简体   繁体   中英

How to remove a tag and its contents from a string in javascript (jQuery)

I have a string like this: Name a,b,c

I got this from the text() method of jQuery, like:

var str = $(this).text();

where $(this) is a wrapper div .

The output of str will be something like: Name a,b,c .

I want a short way to extract just "Name" from the str variable. I don't want to change the DOM eg by using .remove() or .empty() etc. and I also can't use replace() or s ubstr() as the lenghts of the contents in the sup tag is variable.

I have seen some regex and DOM manipulation functions but again this is not something I could use. Any suggestions of what is the most efficient way to do this?

Hello DevMan

I am afraid that this question has already been answered in another post or at least is an initial point for this one. Take a look and see if this covers your inquire. Cheers Mate!

Previous Answered Post

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