newbie question - regular expression

R

rithish

I am trying to implement a String trim function as told in
http://jibbering.com/faq/#FAQ4_16
I must be doing something wrong as I am unable to trim a given string.
Below is the test code. I am on IE6. Can someone point out to where I
am going wrong? Your help is appreciated.

[...snippet...]
<HTML>
<HEAD>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
if ( typeof String.prototype.trim == "undefined" ) {
String.prototype.trim = new Function("return
this.replace(/^\\s+|\\s+$/g,'')") }

function TestStringTrim ()
{
var trim_str = ' string ';
trim_str.trim();
alert ( 'after executing trim : ' + trim_str.length );
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ID='frmTestJs' NAME='frmTestJs'>
<INPUT TYPE='BUTTON' ID='btnTest' NAME='btnTest' VALUE='Test Trim'
onClick='javascript:TestStringTrim();'>
</FORM>
</BODY>
</HTML>
[.../snippet...]

Regards,
Rithish.
 
V

VK

I am trying to implement a String trim function as told in
http://jibbering.com/faq/#FAQ4_16
I must be doing something wrong as I am unable to trim a given string.
Below is the test code. I am on IE6. Can someone point out to where I
am going wrong? Your help is appreciated.

[...snippet...]
<HTML>
<HEAD>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
if ( typeof String.prototype.trim == "undefined" ) {
String.prototype.trim = new Function("return
this.replace(/^\\s+|\\s+$/g,'')") }

function TestStringTrim ()
{
var trim_str = ' string ';
trim_str.trim();
alert ( 'after executing trim : ' + trim_str.length );
}
</SCRIPT>
</HEAD>
<BODY>
<FORM ID='frmTestJs' NAME='frmTestJs'>
<INPUT TYPE='BUTTON' ID='btnTest' NAME='btnTest' VALUE='Test Trim'
onClick='javascript:TestStringTrim();'>
</FORM>
</BODY>
</HTML>
[.../snippet...]

The problem is that the trim() method doesn't change the original
string: it creates new trimmed string and returns it. Try:
alert ( 'after executing trim : ' + trim_str.trim().length );
 
R

rithish

VK said:
The problem is that the trim() method doesn't change the original
string: it creates new trimmed string and returns it. Try:
alert ( 'after executing trim : ' + trim_str.trim().length );

So.. So.. Dumb of me... That's right VK... :eek:)

However, is there a way to change the original string?

Regards,
Rithish.
 
R

rithish

theOriginalString = Change(theOriginalString)Was that a dig at my ribs Evertjan? :eek:)

Does this mean a 'no' then?

Regards,
Rithish.
 
T

Thomas 'PointedEars' Lahn

So.. So.. Dumb of me... That's right VK... :eek:)

However, is there a way to change the original string?

No, string values are immutable. You can only create a new string
value from them.


PointedEars
 
E

Evertjan.

wrote on 15 mrt 2006 in comp.lang.javascript:
Was that a dig at my ribs Evertjan? :eek:)

Does this mean a 'no' then?

It was an answer to your question you skipped in quoting:

So: yes, there is.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top