views:

297

answers:

2

I want to continue one task because my head has some problem with a regular expression. How can it be done with a regular expression and jQuery?

I have this HTML:

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

And I want to get just this text from all this string, every time at the beginning I have

" &nbsp;&gt;&nbsp;&nbsp;&gt;&nbsp;" + Some text ... these special character are not changing every time at the begin .

One way is:

$('.test').html($('.test').html().replace(/&[^;]+;/g, ''));

It works, but I realized that I have at the end some bad characters and I have to remove "\n\n\n" as well

My output:

Presentation Text\n\n\n

But I need the clean text without "\n" or something else, just "Presentation Text"

What can I do to solve this problem?

+1  A: 
$('.test').html($('.test').html().replace(/&[^;]+;|\s+$/g, ''));

This will remove all the whitespace characters (\n, \t, space etc) from the end of the text.

Amarghosh
+1  A: 

Since you're using jQuery, you can use $.trim to remove white-spaces from the beginning and end of your string.

Kobi