View Full Version : Regex replacement not occuring

09-21-2005, 05:40 AM
I am trying to strip all spaces off the end of a string using the following:

element.value = element.value.replace(/[ ]+$/g, "");

This only works if a single trailing space is in the element's value. Otherwise, nothing happens.

09-21-2005, 05:51 AM
Well, it wasn't working five minutes ago but now it is. :(

I think the fact that I am running this on a browser in beta might have something to do with it. :(

09-21-2005, 06:50 AM
Try this:

String.prototype.rtrim = function()
return this.replace(/\s+$/,'');
element.value = element.value.rtrim();

09-21-2005, 07:23 PM
Saw this in the FAQ just now. Sorry for posting redundant questions. :o

What regex engine does JavaScript use? Is it PCRE 5.0+?

09-22-2005, 03:38 AM
JavaScript doesn't "use" a regex engine. Regular Expressions are a native part of the language and part of the specification. Some JavaScript Engines (e.g. KJS) use PCRE. Most (e.g. Spidermonkey, Rhino, Linear etc.) have regex integrated in the scripting engine. JScript has a shared regex engine with VBScript.

09-22-2005, 03:43 PM
Thanks for clearing that up liorean. :)