tags:

views:

66

answers:

4

I am getting a string which is nothing but innerHTML, and so it has instances of  . I need to trim the string such that the trailing   alone are removed. Tried this:

var text;
text = txtInnerHTML.replace(/( )*/g,"");

This removes all instances of   which is not desired.. Only the trailing   instances may be which may be zero or more need to be removed.

+2  A: 

Try txtInnerHTML.replace(/( )+$/g,"");

bogdanvursu
Doesnt work for me.. :(
ria
A: 
text = txtInnerHTML.replace(/( )*$/,"")
Philippe Leybaert
+1  A: 

Use the end of string anchor

text.replace(/( )+$/, '');

I remembered which one is what by thinking "carrots are more important than money". Weird, but it worked for me when I was learning. It basically says ^ is start anchor and $ is end anchor. Probably doesn't make much sense out of localizations that use $ for money value.

alex
A: 

txtInnerHTML.replace(/( |\s)$/,"");

$
Matches the ending position of the string or the position just before a string-ending $newline. In line-based tools, it matches the ending position of any line.
wikipedia

Don't forget about simple space " ". Regular expression above removes   or " " in the end of string

sbmaxx