1

I'm trying to write a regex script that will parse through HTML name attributes and return each nested array as a match. Here's an example:

<input type="text" name="contact[email]" />
<input type="text" name="contact[address][street]" />

I need some javascript regex that will parse those and match them in this way

Match 1: contact Match 2: email
Match 1: contact Match 2: address Match 3: street

Here's the current regex I have:

/(^.*?)(\[(.*?)\])?$/

Thanks!

5
  • What is your regex engine or environment? Commented Jul 9, 2013 at 18:31
  • Are you using the regex on the name attribute or on the entire string of HTML? Commented Jul 9, 2013 at 18:31
  • @Jerry it is Javascript Commented Jul 9, 2013 at 18:33
  • @Explosion Pills I'm using it solely on the name attribute. Commented Jul 9, 2013 at 18:34
  • where does the source text come from? innerHTML? String? Commented Jul 9, 2013 at 18:47

5 Answers 5

3

I think the easiest way to do this is

var str = "contact[email]"
str.match(/\w+/g)
//=> ["contact", "email"]

var str = "contact[address][street]"
str.match(/\w+/g)
//=> ["contact", "address", "street"]
Sign up to request clarification or add additional context in comments.

4 Comments

Yes you're right this turned out to make things so much simpler!
Nice answer! I'm just curious though. Will the spots that say "address" or "email" have an actual address/email or will it be just the word? If it's just the word then you're all set, but if you need to capture an actual email then this will not work.
@Th3BFG, the output is exactly as displayed in my post. My code does nothing to read the value attribute of the INPUT element.
Makes sense. I'm not sure why I was thinking the name would have an email address.
2

What I would do is delimit each of your name with [] So it would go like this:

<input type="text" name="[contact][email]" />
<input type="text" name="[contact][address][street]" />

Then I would use this for the regex:

(?:\[)(.*?)(?:\])

My solution cuts down on the number of operations needed and makes your naming convention a lot more straight forward. Each match would represent a separate entry in the name section

2 Comments

Oh, and if your regex engine requires it, use this: ^(?:\[)(.*?)(?:\])$
I think your regex engine would want this: /(?:\[)(.*?)(?:\])$/g
1

Use the following regex:

/(^[^\[]+)(?=\[)|(([^\[\]]+)(?=\]))/g

Example usage below.

Demo fiddle here.

HTML:

<input id="one" type="text" name="contact[email]" />
<input id="two" type="text" name="contact[address][street]" />

JavaScript:

var regex = /(^[^\[]+)(?=\[)|(([^\[\]]+)(?=\]))/g;

var nameOne = document.getElementById('one').getAttribute('name');
console.log('one: ', nameOne.match(regex));
var nameTwo = document.getElementById('two').name;
console.log('two: ',nameTwo.match(regex));

Output:

one: ["contact", "email"]
two: ["contact", "address", "street"] 

Comments

0
<input type="(?<type>[A-Za-z0-9]+)" name="(?<Name>[A-Za-z0-9\[\]]+)" />

works fine. I attached Image to show how works

enter image description here

as each value will be returned in array after RegexMatch, then you can do this

var first = RegexMatch.Groups["Name"].Split('[')[0];

You will get the first value in the name="contact[address][street]", first = "contact" then you can do

var second = regexMatch.Groups["Name"].Split('[')[1];
then second = "address";

by the way this is for C#, convert to javascript!!

Comments

0

i like to use split() to grab middle-chunks instead of more complex RegExps:

var strFormHTML='<input type="text" name="contact[email]" /><input type="text" name="contact[address][street]" />';
var names = strFormHTML.split(/[\W\w]+? name=\"([^"]+)\"[\w\W]+?/).filter(Boolean).slice(0,-1); 
alert(names); //shows: "contact[email]", "contact[address][street]"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.