Javascript regex returning true.. then false.. then true.. etc [duplicate]

Active3 hr before
Viewed126 times

7 Answers


Meta Stack Overflow , Stack Overflow help chat , Stack Overflow Public questions & answers ,Stack Overflow en español


Also, I don't think you want [^-_] at the front and back. That will allow any character at each end, ie. *plop! would be valid. You're probably thinking of lookahead/lookbehind assertions, but they're not available in JavaScript. (Well, lookahead is supposed to be, but it's broken in IE.) Suggest instead:

load more v

Edit: I've tested the problem in Firefox and Chrome (mac),I have a strange problem with the validation I am writing on a form. It is a 'Check Username' button next to an input. The input default value is the username for example 'betamax'. When I press 'Check Username' it passes the regex and sends the username to the server. The server behaves as expected and returns '2' to tell the javascript that they are submitting their own username.,This is wrong, The last - must be at the beginning or end.,You can omit the g from your RegExp, since you're only testing for one match.

This is my code:


function checkUserName() {
   var userName = $j("#username").val();

   var invalidUserMsg = 'Invalid username (a-zA-Z0-9 _ - and not - or _ at beginning or end of string)';
   var filter = /^[^-_]([a-z0-9-_]{4,20})[^-_]$/gi;
   if (filter.test(userName)) {
         "/account/profile/username_check/", {
            q: userName
         function(data) {
            if (data == 0) {
               $j("#username-search-results").html("Error searching for username. Try again?");
            } else if (data == 5) {
            } else if (data == 4) {
               $j("#username-search-results").html("Username too short or too long.");
            } else if (data == 2) {
               $j("#username-search-results").html("This is already your username.");
            } else if (data == 3) {
               $j("#username-search-results").html("This username is taken.");
            } else if (data == 1) {
               $j("#username-search-results").html("This username is available!");
   } else {

   return false;



<input name="username" id="username" value="{{ user.username }}" />
<input type="button" value="Is it taken?" id="username-search">
<span id="username-search-results"></span>
load more v

Returns all matches of the regular expression against a string.,Whether to test the regular expression against all possible matches in a string, or only against the first.,Whether the regular expression result exposes the start and end indices of captured substrings.,Note that the order of the patterns in the regular expression matters.

let re = /ab+c/i; // literal notation
let re = new RegExp('ab+c', 'i') // constructor with string pattern as first argument
let re = new RegExp(/ab+c/, 'i') // constructor with regular expression literal as first argument (Starting with ECMAScript 6)
load more v

regExp is cloned. If flags is provided, then it determines the flags of the clone.,The regular expression method .test() returns true if regExp matches str:,The body abc – the actual regular expression.,43.5.9 Other methods for working with regular expressions

load more v

You have a form on your website or a dialog box in your application that asks the user for an email address. You want to use a regular expression to validate this email address before trying to send email to it. This reduces the number of emails returned to you as undeliverable.,First, load a bunch of valid and invalid sample data into the tool. In this case, that would be a list of valid email addresses and a list of invalid email addresses.,If you want to avoid sending too many undeliverable emails, while still not blocking any real email addresses, the regex in Top-level domain has two to six letters is a good choice.,Then, write a simple regular expression that matches all the valid email addresses. Ignore the invalid addresses for now. ‹^\S+@\S+$› already defines the basic structure of an email address: a local part, an at sign, and a domain name.

^ \S + @\ S + $
A\ S + @\ S + \Z
load more v

Returns the number of full pattern matches (which might be zero), or false on failure. , Searches subject for all matches to the regular expression given in pattern and puts them in matches in the order specified by flags. ,preg_match() - Perform a regular expression match,preg_match_all — Perform a global regular expression match

<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test
load more v

--dry-run Prints the file paths that would be copied by this command. This flag does not copy the actual files.,--disable-auto-decoding False by default to enable automatic decoding of illegal chars on Windows. Can be set to true to disable automatic decoding.,Upload files and directories to Azure Storage account and set the query-string encoded tags on the blob.,Upload a single file by using OAuth authentication. If you have not yet logged into AzCopy, run the azcopy login command before you run the following command.

If you set an environment variable by using the command line, that variable will be readable in your command-line history. Consider clearing variables that contain credentials from your command-line history. To keep variables from appearing in your history, you can use a script to prompt the user for their credentials, and to set the environment variable.

azcopy copy[source][destination][flags]
load more v

Other "regex-duplicate" queries related to "Javascript regex returning true.. then false.. then true.. etc [duplicate]"