|
Answer» I am trying to extract "67890" from "param1=12345¶m2=67890".
When I USE the following code, I get a blank page. Code: [SELECT]<html> <head> <title>JavaScript Test</title> </head> <body> <script type="text/javascript"> var string1; var string2; var string3; string1 = "param1=12345¶m2=67890"; string2 = string1.match(/param2=[0-9]{5}/); string3 = string2.match(/[0-9]{5}/); document.write("string1: " + string1 + "<br />string2: " + string2 + "<br />string3: " + string3); </script> </body> </html>
If I comment out "string3 = string2.match(/[0-9]{5}/);" I get the following result:
Code: [Select]string1: param1=12345¶m2=67890 string2: param2=67890 string3: undefined
Why does the second match() not work?
EDIT: I am sorry for posting this in Networking. I must have been so distracted by my problem that I clicked on the wrong board. Can someone please move it to Web Design?I THINK I have figured it out. As I currently UNDERSTAND it, match() returns the result as an array EVEN if there is only one result. To run another match() on the results of the first match(), I have to define which array entry to use.
Here is my new code. Code: [Select]<script type="text/javascript"> var string1 = "param1=12345¶m2=67890"; var string2 = string1.match(/param2=[0-9]{5}/); var string3 = string2[0].match(/[0-9]{5}/); document.write("string1: " + string1 + "<br />string2: " + string2 + "<br />string3: " + string3); </script>
Here are the results of that code. Code: [Select]string1: param1=12345¶m2=67890 string2: param2=67890 string3: 67890
After learning about the array, I was able to condense my code the the following: Code: [Select]<script type="text/javascript"> var string1 = "param1=12345¶m2=67890"; var string2 = string1.match(/param2=([0-9]{5})/); document.write("string1: " + string1 + "<br />string2: " + string2[1]); </script>
|