0

Can anyone help me with this regex? I need something which allow: 0-9 a-z A-Z spaces commas hyphens apostrophes and these other special characters: _@=.` plus at the same time set characters limit into it.

I've got this now but it's throwing invalid regular expression exception.

var regex = /^\s*([0-9a-zA-Z\s\,\-\(\)\'\_\@\~\&\*\=\.\`]{1,35}+)\s*$/;
        return this.optional(element) || value.match(regex);
       }, "Please enter a valid name"    );

Thanks for any help!

6
  • 1
    {1,35}+ Whats this expected to do? Either the one, or the other. Both together doesn't make a lot of sense. Commented Jan 21, 2019 at 6:10
  • sorry, im new to regex .. i added {1,35} thinking that it will help to validate the character limit (minimum 1 and max 35 characters) Commented Jan 21, 2019 at 6:13
  • Yeah, makes sense. But then adding + which means one or more is pointless. Commented Jan 21, 2019 at 6:16
  • @tkausl Plus sign in this context doesn't mean one or more. It changes the previous quantifier to possessive. Check this Commented Jan 21, 2019 at 6:18
  • i see, i didn't know that the + sign means so initially, thanks for the help! it works perfectly now :) Commented Jan 21, 2019 at 6:19

2 Answers 2

1

Remove + after {1,35} and escape only special characters:

var regex = /^\s*([0-9a-zA-Z\s,\-()'_@~&*=.`]{1,35})\s*$/;

console.log(regex.test(" asdfghjkl "))
console.log(regex.test(" asdf'&@() "))
console.log(regex.test(" asdfghjklasdfghjklasdfghjklasdfghjklasdfghjkl "))

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

Comments

0

Anything Meta Sequence Combo

It looks like you want anything. If so use:

/[\S\s]{1,35}/g

The brackets [...] means one character within it is a match.

\s is any space.

\S is any non-space.

and {1,35} means one to thirty-five consecutive matches of whatever preceded it.

Demo

Features the way to get the first 1 to 35 characters and also every 1 to 35 characters

var str = `1234567~!@#$%^&*e()_+_{}{:>><rthjokyuym.,iul[.<>LOI*&^%$#@!@#$%^&*()_+_{}{:>><KJHBGNJMKL>|}{POIUY~!@#$%^&*(+_)O(IU`;

var rgx = /[\s\S]{1,35}/g;

var res = rgx.exec(str);

console.log(`==== The first 35 characters ===========================`);
console.log(res[0]);
console.log(' ');
console.log(`==== OR for every 1 to 35 characters ===================`);
while (res !== null) {
  console.log(res[0]);
  var res = rgx.exec(str);
}
.as-console-wrapper {
  min-height: 100%;
}

div div div.as-console-row:first-of-type,
div div div.as-console-row:nth-of-type(4) {
  color: tomato
}

Comments

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.