Welcome to WebmasterWorld Guest from 54.146.239.96

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Regex Question

Javascript, but you guys know regex

     

Greven

5:35 am on Dec 19, 2008 (gmt 0)

10+ Year Member



This is a copy of post in the javascript forum. I haven't hear anything yet, so figured I'd try with the people that know regular expressions best.

I have a string that I'm matching on, and using the results to generate a custom error message from the one provided. I have a group that should match on values inside a repeating non-matching group. Code as follows:

String to search:


Element '{http://example.com/dev}emailCredit' is unexpected according to content model of parent element '{http://example.com/dev}promotion'.
Expecting: {http://example.com/dev}programmingCredit, {http://example.com/dev}cfppCredit.

Regular Expression:

/^Element '.*' is unexpected according to content model of parent element '.*'.\s*Expecting:(?: {[\w:\/\-]*}(\w*)[,]?)+\w*\.\s*$/

Code executing above

var reqReg = new RegExp(/^Element '.*' is unexpected according to content model of parent element '.*'.\s*Expecting:(?: {[\w:\/\-]*}(\w*)[,]?)+\w*\.\s*$/);
var values = reqReg.exec(exmpleString)//String listed as String to search
errorContainer.appendChild(document.createTextNode(values));

What I would expect to get back as text inside of errorContainer would be:

Element '{http://example.com/dev}emailCredit' is unexpected according to content model of parent element '{http://example.com/dev}promotion'.
Expecting: {http://example.com/dev}programmingCredit, {http://example.com/dev}cfppCredit.
,programmingCredit,cfppCredit

But what I am getting:

Element '{http://example.com/dev}emailCredit' is unexpected according to content model of parent element '{http://example.com/dev}promotion'.
Expecting: {http://example.com/dev}programmingCredit, {http://example.com/dev}cfppCredit.
,cfppCredit

So my question is: Should it not catch on all the nested groups and return each one? It appears that it's only returning the last one.

janharders

1:04 pm on Dec 21, 2008 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



I'm not completely sure I understand what you're trying to accomplish. Sure enough, I'm not a regexp wizard, but I guess that it happens because you only have one group that will hold the values. You'd have to iterate over the results, which won't work because you're using nested groups, so all are matched.
I'd match the Expecting-part and apply another regexp on that part matching the sub-parts. Oh, and you should probably escape the { in your regexp.

krugs

4:33 pm on Dec 21, 2008 (gmt 0)

5+ Year Member



To me the regexp looks like it should not match anything unless \w is different in javascript than it is in perl. In perl there is no dot (.) in the \w character class so domain names will not be matched:

example.com

maybe it should be:

[\w.:\/-]

note that in perl the order of the characters inside the square brackets is ignored and a dot does not need escaping.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month