View Full Version : replace() explaination...
Eternity Angel
09-29-2002, 04:17 PM
Okay, I haven't a clue what this is, it's fine and all if it works on multiple levels, but I like to KNOW what things are, before I go off using them like wild fire.
So, I am asking if anyone can EXPLAIN this snippet of code, and why it works:
<script>
var badtext = "this, is, a, test, to, see, what, the, heck, is, going, on...";
var tester = badtext.replace(/,/g,"*");
document.write(tester);
</script>
It replaces all commas with *'s... What exactly is "/,/g"?
whammy
09-29-2002, 05:01 PM
That is a Regular Expression (see my signature if you want to know how much I like/use them ;)).
What it is telling javascript in English is this:
"Replace every instance of a comma with an asterisk in the string badtext."
Here's a breakdown:
/ = start pattern
, = pattern to look for in the string badtext
/ = end pattern
g = global (anywhere the pattern match occurs in the string)
"*" = what to replace the pattern with if found
RegEx's (for short) are one of the most powerful tools in programming. They look difficult at first, but once you understand the basics and mess around with it for awhile, they are awesome.
Check out these links:
http://developer.netscape.com/docs/manuals/js/client/jsguide/regexp.htm
http://www.siteexperts.com/tips/functions/ts23/page1.asp
to start with. :)
Eternity Angel
09-29-2002, 05:28 PM
Wow... This looks complicated...
I think I stumbled off into some odd new world...
Thanks for the help though!!
whammy
09-29-2002, 07:10 PM
Don't let them scare you!
I put a couple of examples below on how you can use a regular expression to either remove spaces, or trim extra spaces from a variable... so you can do very simple things with them, or very complex things with them (and the right regular expression can validate or manipulate anything)!
Don't be afraid to copy and paste the code below into notepad and save it as an .htm file to play around with:
<script language="javascript" type="text/javascript">
<!--
//***********************************************
/*
Remove spaces from input...
*/
str1 = "someone @ somewhere . com"
str1 = str1.replace(/\s+/g,"");
document.write(str1);
//***********************************************
document.write("<br />")
//***********************************************
/*
Trim spaces from the beginning of a string
*/
str2 = " This is a test!";
str2 = str2.replace(/^\s+/,"");
document.write(str2);
//***********************************************
document.write("<br />")
//***********************************************
/*
Trim spaces from the end of a string
*/
str3 = "This is a test! ";
str3 = str3.replace(/\s+$/,"");
document.write(str3);
//***********************************************
document.write("<br />")
//***********************************************
/*
Remove extra spaces from within a string
and replace them with ONE space
*/
str4 = "This is a test!";
str4 = str4.replace(/\s+/g," ");
document.write(str4);
//***********************************************
document.write("<br />")
//***********************************************
/*
Here's how to combine the last three
examples into one statement!
*/
str5 = " This is a test! ";
str5 = str5.replace(/^\s+/,"").replace(/\s+$/,"").replace(/\s+/g," ");
document.write(str5);
//***********************************************
// -->
</script>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.