0
Checking against a pattern
How to check an input field against a certain pattern For example: To check an email input against (example@example.com)
8 Answers
+ 4
Well, you can do so with a regular expression:
if (/^[\w\d]+@\w+\.com$/.test(inputString))
console.log("Good enough");
else
console.log("Not correct");
I did not just slam my head on my keyboard, this regular expression will try to match this:
^: Start of the string
[\w\d]+: One or more word character(s)/digit(s)
@: @ character
\w+: One or more word character(s)
\.: . character
com: These three charactere exactly
$: End of the string
+ 1
I'm a fan of www.regex101.com
Here is one I setup to work with a variety of email formats:
https://regex101.com/r/pt5qJh/4
Using this RegEx:
^([\w-.]+)@([\w-]+)(\.[\w.-]+)$
Hope this helps.
0
thanks i think i am going to see videos on regular expressions
0
i wanna ask you if i wanna put it as a part of a comparing operation like
if(#id == )
should i put the rgex inside queotes or what
0
No, you cannot compare to regular expressions like that.
If you looked at it; regular expressions are enclosed in slashes, so this is a valid regular expression: /^/. There are a few functions you can use to use regular expressions, for example:
RegExp.prototype.test
RegExp.prototype.exec
String.prototype.match
String.prototype.replace
(and so on)
but test is the most logical, as it returns a simple boolean
0
ohh thanks another question if u do not mindđ
why in ur example /^[\w\d]/
why did put \d even though that
\w means letters and digits based on what i watched
0
Because I am stupid
0
đ
all of this and stupid
u have to forgive yourself sometimes i think đ