David Harrison
06-19-2003, 05:00 PM
How do I detect whether a string has a certain character in it and then if so, how would I find where it is in the string?
|
Click to See Complete Forum and Search --> : silly strings David Harrison 06-19-2003, 05:00 PM How do I detect whether a string has a certain character in it and then if so, how would I find where it is in the string? Vladdy 06-19-2003, 05:15 PM Learn and enjoy the power: http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/regexp.html#1010922 David Harrison 06-20-2003, 04:09 PM How would I see if a string contains anything other than numbers and if so remove all of the non numbers. Also if the sting to start with didn't have any numbers in then give the string a default value of 1000. Charles 06-20-2003, 04:20 PM <script type="text/javascript"> <!-- String.prototype.getNumber = function () {if (!/\d/.test(this)) return 1000; return this.replace(/\D/g, '')} alert ('fee31fie41foe59fum27'.getNumber()); // --> </script> David Harrison 06-20-2003, 04:27 PM Thanks for the script it works great but could you give me a brief explanation as to how it works please. AdamBrill 06-20-2003, 05:10 PM Like this it is a bit easier to read(and I added comments):<script type="text/javascript"> <!-- String.prototype.getNumber = function () { \\creates a String.getNumber function if (!/\d/.test(this)) \\if there are aren't any numbers, do this return 1000; \\change this to whatever you want it to return if there aren't any number in the string return this.replace(/\D/g, '') \\if there are numbers, run a RegEx to change all of the non-digits to '' } alert ('asasfd'.getNumber()); \\run the getNumber function on the string // --> </script> Charles 06-20-2003, 06:01 PM I like this one even better: <script type="text/javascript"> <!-- String.prototype.getNumber = function () {return /\d/.test(this) ? this.replace(/\D/g, '') : 1000} alert ('fee3.1fie41foe59fum27'.getNumber()); // --> </script> AdamBrill 06-20-2003, 11:00 PM LoL... I think your just trying to confuse lavalamp... :D David Harrison 06-21-2003, 12:52 PM No I spotted the mod., it's like changing: if(a==b){a=3;} else{a=4;} into: a=(a==b)?3:4; webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |