Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript preg_match_all

Status
Not open for further replies.

richardko

Programmer
Jun 20, 2006
127
US
hi,
I have a string containing html where i need to use regex to match certain elements.
the string can be
Code:
ajkshajda
<form name="form1"></form>
aksdhaskjashd
<form name="form2"></form>
lakjdkjasjas
"
how would you match all the <form name=""> elements and get the "name" of the form?
thanks
 
I tried the code below without success. any ideas:
Code:
var regex = new RegExp("form name=(.*[^>])","g");

        var match = regex.exec("<form name=form1><form name=form2><form name=form3");
        if (match == null) {
                alert("No match");
        } else {
                var string = "matched at position " + match.index + ":\n";
                string = "length of arr: ".match.length + "\n";
                string = string + "string matched: " + match[0] + "\n";
                if (match.length > 0) {
                        for (var i = 1; i < match.length; i++) {
                                string = string + match[i] + "\n";
                        }
                }
                document.getElementById("tt").innerHTML = string;
                //alert(string);
        }
 
The example below works when there is line break in the string. But how do I modify the regex if there is no linebreak in the string.
Code:
var backRefs = new Array();

	var testString = '<form name=form1>\n'+
			'kajhsdskjadas</form>\n'+
			'<form name=form2>\n'+
			'aksjashkjashjkas</form>\n'+
			'<form name=form3>\n'+
			'lkuaksdksa</form>\n';
 	var re = /form name=(.*[^>])/g;

	var matches;

	while (matches = re.exec(testString)) {
		backRefs.push(matches);
	}
	string += "LLLLLLL: "+backRefs.length;
	document.getElementById("tt").innerHTML = string;
 
[tt] var re = [blue]/<form\s+.*?\s*name\s*=\s*["']?(.*?)["']?[\s>]/gi;[/blue]
var matches;

while (matches = re.exec(testString)) {
backRefs.push(matches[blue][1][/blue]);
}
[/tt]
 
how would you match all the <form name=""> elements and get the "name" of the form?

This is a way to get every form name.

Code:
var formArray = document.getElementByTagName("form")
var formArrayLength = formArray.length;
for (a = 0; a < formArrayLength; a++) {
   alert(formArray[a].name);
}

[monkey][snake] <.
 
thank you for the reply monksnake.
however its not a document that I am parsing through. Its a html string.
 
Ahh got ya.

Let me test what I did real fast and I'll post what I got.


[monkey][snake] <.
 
You can assign that html string to an invisible iframe and use monksnake's technique.

Cheers,
Dian
 
I pretty much copied your code, but I set it up to return each name whether or not there are line breaks. This code assumes you DON'T put "" around the name, which you really should and it assumes you only use letters and numbers for the name.

Code:
<script type="text/javascript">
var testString = '<form name=form1>ajhsdskjadas</form><form name=form2>\n'+
            'aksjashkjashjkas</form>\n'+
            '<form name=form3>\n'+
            'lkuaksdksa</form>\n';
     var re = /form name=[A-Za-z0-9]+/g
     while (matches = re.exec(testString)) {
        alert(String(matches).split("=")[1]);    
     }
</script>


[monkey][snake] <.
 
Thanks for all the replies. I ended up using "monksnake" method but another question arose. How do I match nested tags. For example this is the string that I have:

Code:
<span name="form1"><input type="text" name="" value=""><input type="checkbox" name="" value=""><span name="form2"><input type="radio"><input type="text"></span>somasjgjasha</span>teskjhajs<span>test text</span>a.jkahsk

how do i match only the data inside <span name="form1"> and its closing </span> tag
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top