Post by Jay on Apr 28, 2005 22:40:48 GMT
Preface
---
So you've seen people using this function called substring, and your wondering just exactly how it works. In this tutorial I will cover the very basics of the function plus a handy way of using it in your document. String functions are generally used to grab a certain piece of data between two pieces of data (somewhat similar to RegEx, but will grab any number or letter as apposed to the limited use of RegEx). For example, let's say your document contains the HTML of:
Advanced Coders: You should know that this particular string of data will grab the amount of Private Messages your members have. (\d+) will contain the exact number. We are also stating that the following word will be either "are" or "is".
Beginner Coders: You now know this, take note. You'll find yourself using this method to grab the amount of PM's quite alot. \s+ are optional characters that represent a space. You can use "\s", "\s+", or a space, " ". Whichever you want to use is fine.
Okay, now onto the goodies...
Now that we've grabbed the data we want, let's convert it to Javascript. Start with your handy script block. =]
Declare a variable that will grab an element by the tagname of "TD".
Declare an if statement that will search for the third TD of your document. Remember, JS counts with 0, so use 0 accordingly.
Moving on we see the inner HTML of the third tag being the example at the beginning of the tutorial that I used. This is how we can obtain the amount of PM's a user has. Now let's declare another variable and write out our RegEx statement using RegExp.$1
Finally, we can use document.write to write out our substring.
Your full code should be as follows.
Explanation
---
Substring's proper syntax is:
variable.substring(firststring, laststring). If you look inside the document.write statement, you will notice I used substring(6, 13). The return is the difference between the two + 1. For example, I would count out 6 characters (including spaces), and on the seventh would be the start of the string. The difference between the two will be the word/number/character your trying to write. Being the difference between my two numbers is "7", it will write the word "Private" from my substring variable.
Substring can be used in many ways. This is just the most simple of forms. Experiment with it for awhile, and you'll be grabbing the right data in no time. =]
---
So you've seen people using this function called substring, and your wondering just exactly how it works. In this tutorial I will cover the very basics of the function plus a handy way of using it in your document. String functions are generally used to grab a certain piece of data between two pieces of data (somewhat similar to RegEx, but will grab any number or letter as apposed to the limited use of RegEx). For example, let's say your document contains the HTML of:
,\s+(\d+)\s+(are|is)\s+new
Advanced Coders: You should know that this particular string of data will grab the amount of Private Messages your members have. (\d+) will contain the exact number. We are also stating that the following word will be either "are" or "is".
Beginner Coders: You now know this, take note. You'll find yourself using this method to grab the amount of PM's quite alot. \s+ are optional characters that represent a space. You can use "\s", "\s+", or a space, " ". Whichever you want to use is fine.
Okay, now onto the goodies...
Now that we've grabbed the data we want, let's convert it to Javascript. Start with your handy script block. =]
<script type="text/javascript">
<!--
//-->
</script>
Declare a variable that will grab an element by the tagname of "TD".
var getTag = document.getElementsByTagName("td");
Declare an if statement that will search for the third TD of your document. Remember, JS counts with 0, so use 0 accordingly.
if(getTag[2].innerHTML.match(/,\s+(\d+)\s+(are|is)\s+new/)) {
Moving on we see the inner HTML of the third tag being the example at the beginning of the tutorial that I used. This is how we can obtain the amount of PM's a user has. Now let's declare another variable and write out our RegEx statement using RegExp.$1
var subStringTest = "Total Private Messages:\n " + RegExp.$1;
Finally, we can use document.write to write out our substring.
document.write(subStringTest.substring(6, 13));
}
Your full code should be as follows.
<script type="text/javascript">
<!--
var getTag = document.getElementsByTagName("td");
if(getTag[2].innerHTML.match(/,\s+(\d+)\s+(are|is)\s+new/)) {
var subStringTest = "Total Private Messages:\n " + RegExp.$1;
document.write(subStringTest.substring(6, 13));
}
//-->
</script>
Explanation
---
Substring's proper syntax is:
variable.substring(firststring, laststring). If you look inside the document.write statement, you will notice I used substring(6, 13). The return is the difference between the two + 1. For example, I would count out 6 characters (including spaces), and on the seventh would be the start of the string. The difference between the two will be the word/number/character your trying to write. Being the difference between my two numbers is "7", it will write the word "Private" from my substring variable.
Substring can be used in many ways. This is just the most simple of forms. Experiment with it for awhile, and you'll be grabbing the right data in no time. =]