0

Who can help me with regular expression? I need to remove a part of a string with jQuery, but I don't have much experience in regex.

<div class="test" >
    &nbsp;&gt;&nbsp;&nbsp;&gt;&nbsp;Test Text
</div>

I want to remove &nbsp;&gt;&nbsp;&nbsp;&gt;&nbsp; and to leave just the text.

2
  • 2
    Could you please use more descriptive titles. Commented Mar 3, 2010 at 21:01
  • See my edit to your previous question. stackoverflow.com/questions/2374579/jquery-string-find/2374759. Submitting a new question which is basically a reframing of your previous problem is redundant. You should edit and continue to follow your first post. Commented Mar 3, 2010 at 22:24

2 Answers 2

4
$('.test').html($('.test').html().replace(/&[^;]+;/g, ''))
Sign up to request clarification or add additional context in comments.

5 Comments

it is adding \n\n\n at the end
I answered this in your previous question. .text().replace(/(^\s+|\s+$|(.*)>\s*)/g,"")
Color me skeptical, but is the code you're testing against identical to what you have in your question? This code can't even introduce new characters, it only replaces matches with empty strings.
if @ghoppe is right, it sounds like you already had the line breaks there.
@theraccoonbear The line breaks follow the text before the ending </div> tag. Your regex doesn't take that into account. So Alex used poor phrasing. It's not adding anything. It's just not removing what he wants removed. :)
3

Like this:

$('.test').html(function(i, old) { return old.replace(/&(nbsp|gt);/g, ''); });

EDIT: Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.