2

I want to verify if a value checks one of following formats : ..A , B.. , A..B

and also retrieve the values: (null, A) , (B, null) , (A, B)

This is my code:

var regexRange = new RegExp("^([a-zA-Z0-9]+)\.\.([a-zA-Z0-9]*)$|^[a-zA-Z0-9]*\.\.([a-zA-Z0-9]+)$");

function getRangeValues(value) {
    var from = null;
    var to = null;
    var matches = regexRange.exec(value);
    if (matches !== null) {
        if (matches[3] !== undefined) {
            to = matches[3];
        }
        else if(matches[1]!==undefined && matches[1]!=='') {
            from = matches[1];
            if (matches[2] !== undefined && matches[2] !== '') {
                to = matches[2];
            }
        }
    }    
    var range = { From: from, To: to };
    return range;
}

Value: 1233 => From=12, To=null

I don't understand why i get this incorrect behavior, for other use cases it seems to work.

3
  • "I don't understand why i get this incorrect behavior" - Which incorrect behaviour? Are you talking about an input of "1233"? Commented Jul 2, 2013 at 14:14
  • Yes, that is exactly what i am talking about Commented Jul 2, 2013 at 14:15
  • Using the backslashes indeed seems to do the trick. Make it an answer please. Commented Jul 2, 2013 at 14:20

4 Answers 4

1

I think the problem is that you didn't escape the backslashes in the string literal that you created the regex from. Inside a string literal you need \\.\\. if you want the resulting regex to have \.\.:

var regexRange = new RegExp("^([a-zA-Z0-9]+)\\.\\.([a-zA-Z0-9]*)$|^[a-zA-Z0-9]*\\.\\.([a-zA-Z0-9]+)$");

Single backslashes in a string are ignored if they're not followed by a character that needs escaping, so you were creating a regex that contained .. which matches any two characters. Easier to use a regex literal:

var regexRange = /^([a-zA-Z0-9]+)\.\.([a-zA-Z0-9]*)$|^[a-zA-Z0-9]*\.\.([a-zA-Z0-9]+)$/;

(Of course there could be other issues, but that one jumped out at me.)

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it this way instead.You can split the input and then validate it

var output=value.split("..");
var from=output[0];
var to=output[1];
if(from.length>=1 || to.length>=1)
{
if(from.search(/[^a-zA-Z0-9]/)==-1 && to.search(/[^a-zA-Z0-9]/)==-1)//valid
else //invalid
}
else//invalid

1 Comment

"Why use regex", indeed :)
1

I believe your regex can be simplified. Consider this code:

str='..A , B.. , A..B';
re = /(\w*)\.\.(\w*)/g;
var arr = [];
var match;
while (match = re.exec(str))
   arr.push([ match[1]=="" ? null:match[1], match[2]=="" ? null:match[2] ]);
console.log(arr);
// OUTPUT: [[null, "A"], ["B", null], ["A", "B"]]

Comments

0

I think you can simplify things a lot:

^([a-zA-Z0-9]+)?\.\.([a-zA-Z0-9]+)?$

would match all three cases you proposed and capture everything in the correct groups (if present). However, it would also match .. which you probably don't want. So, we can add a positive lookahead assertion to make sure at least one alnum character is present:

^(?=.*[a-zA-Z0-9])([a-zA-Z0-9]+)?\.\.([a-zA-Z0-9]+)?$

Then you can do

var myregexp = /^(?=.*[a-zA-Z0-9])([a-zA-Z0-9]+)?\.\.([a-zA-Z0-9]+)?$/;
var match = myregexp.exec(subject);
if (match != null) {
     var from = match[1]
     var to = match[2]
}

2 Comments

Thanks for your effort, but it doesn't seem to work. I get undefined, undefined for A, A.., A..B
@gigi: Did you use the literal regex constructor in my code, or did you paste the regex into your string (which has the problem with backslashes as noticed by nnnnnn)?

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.