2

I would like to validate residence address in the JavaScript using regex, but I dont know much about the regex, what I have tried is to build my own regex (/^[a-zA-Z\s](\d)?$/) but it doesn't seem to work properly.

What I want to achieve is to allow letters, spaces and at least one digit (thats required) and also there should be a possibility to insert the slash / but it shouldnt be required.

Could anyone help me with that?

1 Answer 1

5

I'll get you started, but you'll find yourself becoming more specific as you get accustomed to regular expressions. First, let's examine your regex:

/^[a-zA-Z\s](\d)?$/

The essential thing to note here is that this regex will only match, at most, a two-character string! The character class, [ ... ], matches a single character: in your case, letters and whitespace. You need to combine this with what's called a quantifier, e.g. * meaning "zero or more", + meaning "one or more", and ? meaning "zero or one". You've used a quantifier elsewhere, (\d)?, where you're saying "zero or one" digit character. But what you really want looks more like this:

/^[a-zA-Z\s\d\/]+$/

Here, we're saying "one or more" letters, whitespace, digits, or slashes (note that the forward slash must be escaped using a backslash).

Finally, you say want to require "at least one" digit. This can be achieved with a more advanced construct in regular expressions, called "lookaround assertions." You want this:

/^(?=.*\d)[a-zA-Z\s\d\/]+$/

That, in particular, is a positive lookahead assertion, which you can research yourself. An alternate way of doing this, without lookahead assertions, would be:

/^[a-zA-Z\s\d\/]*\d[a-zA-Z\s\d\/]*$/

This is obviously more complicated, but when you're able to understand this, you know you're well on your way to understanding regular expressions. Good luck!

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

9 Comments

Wow. Thats a briliant answer, thank you so much for your explanation!
By the way: The above regex examples should work in PHP's preg_match or it should be rewritten then? I know that PHP's regex and the JS one, are kinda similar, but when it comes to unicode regex - JavaScript got problems with that.
@Scott - The above regular expressions should work the same in PHP as they do in JS, except that in JS, for example using the replace method, you wouldn't pass in the expression as a (quoted) string, while in PHP you would.
Oh, okay, thank you. Now I'm trying to build another regex on what you have wrote, it should check if passed string has at least one digit (if yes - pass, otherwise throw error) so I should go like this /^(?=.*\d)+$/ then ?
@Scott - One correction, first: /^(?=.*\d)$/ (drop the quantifier). But actually a better solution here is simply /\d/! See, a lookahead assertion was useful in the previous case because you were making a sort of "AND" statement, i.e. yada yada characters AND at least one digit character. The "zero-width" nature of lookahead assertions lets you "peek" ahead, yet "retain your position" in the string; thus you can check more than one condition. In your latest case however you need only to check a single condition: the simplest is looking for a \d and removing the anchors ^ and $.
|

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.