Alphanumeric, Dash and Underscore But No Spaces Regular Expression Check JavaScript

Alphanumeric, dash and underscore but no spaces regular expression check JavaScript

However, the code below allows spaces.

No, it doesn't. However, it will only match on input with a length of 1. For inputs with a length greater than or equal to 1, you need a + following the character class:

var regexp = /^[a-zA-Z0-9-_]+$/;
var check = "checkme";
if (check.search(regexp) === -1)
{ alert('invalid'); }
else
{ alert('valid'); }

Note that neither the - (in this instance) nor the _ need escaping.

Regular Expression for Alphanumeric, Hyphen and Space

try this

^([-A-Za-z0-9]){1,20}$

or this

^([A-Za-z0-9-]){1,20}$

I think you are using JS. It should work. Check here. If you want to include space also then add a space in the character class

^([-A-Za-z0-9 ]){1,20}$

Always remember that - in character class is used for denoting the range usually. So if you want to include hyphen also you should either add it to the beginning or end of the character class or \ it as suggested in other answer

Regex for Alphanumeric Values and not two consecutive underscores underscore

You might be looking for

^(?!_|\d)(?!.*__)(?!.*_$)\w+$

See a demo on regex101.com.


In JavaScript:

const regex = /^(?!_)(?!.*__)(?!.*_$)\w+$/gm;
const str = `_
A
abcd
_
123
some_spaces
not_any_spaces__
1test34
correct_thing`;
let m;

while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}

// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`${match}`);
});
}

Regular expression to match alphanumeric, hyphen, underscore and space string

Use a simple character class wrapped with letter chars:

^[a-zA-Z]([\w -]*[a-zA-Z])?$

This matches input that starts and ends with a letter, including just a single letter.

There is a bug in your regex: You have the hyphen in the middle of your characters, which makes it a character range. ie [9-_] means "every char between 9 and _ inclusive.

If you want a literal dash in a character class, put it first or last or escape it.

Also, prefer the use of \w "word character", which is all letters and numbers and the underscore in preference to [a-zA-Z0-9_] - it's easier to type and read.

Regex for allowing alphanumeric,-,_ and space

Try this regex:

/^[a-z\d\-_\s]+$/i

REGEX : accept only alphanumeric characters and spaces except the spaces at the begining or ending of expression

I would write the regex as the following in case insensitive mode:

^[a-z0-9](?:[a-z0-9 ]*[a-z0-9])?$

This requires a leading alphanumeric character, along with optional alphas or spaces in the middle, ending also with an alphanumeric character, at least for the case where the length be 2 or more characters.

Sample code:

var inputs = ["   aaaa978aa", "aaaaaa      ", "Alphanumeric, Dash and Underscore But No Spaces Regular Expression Check JavaScriptAAAa", "aaaaaaa  aaa", "68776  67576", "aAAAa756Gaaa"];
inputs.forEach(x => console.log(x + (/^[a-z0-9](?:[a-z0-9 ]*[a-z0-9])?$/i.test(x) ? " : match" : " : fail")));

regex to match only alphanumeric, hyphen, underscore, and period, with no repeating punctuation

Use:

^[a-z0-9]+(?:[._-][a-z0-9]+)*$

Explanation:

^                   # beginning of line
[a-z0-9]+ # 1 or more alphanum
(?: # start non capture group
[._-] # period, underscore or hyphen
[a-z0-9]+ # 1 or more alphanum
)* # end group, may appear 0 or more times
$

Demo

var test = [    'this.is.Valid',    'also_a_valid_1',    'Me-too.im_an-ugly.but_vALid-5tring',    '-this..should..not.be.valid....',    '..THIS__.-also-should..fail-',    'why..IS_regex--so.confusing-for-n0obs',    'h',    'sTrInG',];console.log(test.map(function (a) {  return a+' :'+/^[a-z0-9]+(?:[._-][a-z0-9]+)*$/i.test(a);}));

regex to match alphanumeric and hyphen only, strip everything else in javascript

If you want to remove everything except alphanum, hypen and underscore, then negate the character class, like this

String = String.replace(/[^a-zA-Z0-9-_]+/ig,'');

Also, ^ and $ anchors should not be there.

Apart from that, you have already covered both uppercase and lowercase characters in the character class itself, so i flag is not needed. So, RegEx becomes

String = String.replace(/[^a-zA-Z0-9-_]+/g,'');

There is a special character class, which matches a-zA-Z0-9_, \w. You can make use of it like this

String = String.replace(/[^\w-]+/g,'');

Since \w doesn't cover -, we included that separately.

Quoting from MDN RegExp documentation,

\w

Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_].

For example, /\w/ matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."



Related Topics



Leave a reply



Submit