Regex for allowing alphanumeric,-,_ and space
Try this regex:
/^[a-z\d\-_\s]+$/i
Regex: Match alphanumeric and spaces with exception of leading spaces
You can use this regex
^[^a-z0-9]+|[^a-z0-9 ]+|\s{2,}|[^a-z0-9]+$
^[^a-z0-9]+
- Matches anything else than alphanumeric at start of string[^a-z0-9 ]+
- Matches anything else than alphanumeric and space\s{2,}
- Matches two or more consecutive space characters[^a-z0-9 ]+$
- Matches anything else than alphanumeric at end of string
Demo
Regular Expression for alphanumeric and space
Because Prajeesh only wants to match spaces, \s will not suffice as it matches all whitespace characters including line breaks and tabs.
A character set that should universally work across all RegEx parsers is:
[a-zA-Z0-9 ]
Further control depends on your needs. Word boundaries, multi-line support, etc... I would recommend visiting Regex Library which also has some links to various tutorials on how Regular Expression Parsing works.
Regex to match alphanumeric and spaces
just a FYI
string clean = Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);
would actually be better like
string clean = Regex.Replace(q, @"[^\w\s]", string.Empty);
Regex for allowing alphanumeric and space
Just add a space; ^[a-zA-Z0-9 ]*$
Increase your google-fu: https://www.google.com/search?q=regex+allow+spaces
I'm not sure how or what you tried googling but there's heaps of results to your question... Including this StackOverflow Post where your question has been answered in great detail.
Regex to allow alphanumeric, spaces, some special characters
You seem to need to avoid matching strings that only consist of digits and make sure the strings start with an alphanumeric. I assume you also need to be able to match empty strings (the original regex matches empty strings).
That is why I suggest
^(?!\d+$)(?:[a-zA-Z0-9][a-zA-Z0-9 @&$]*)?$
See the regex demo
Details
^
- start of string(?!\d+$)
- the negative lookahead that fails the match if a string is numeric only(?:[a-zA-Z0-9][a-zA-Z0-9 @&$]*)?
- an optional sequence of:[a-zA-Z0-9]
- a digit or a letter[a-zA-Z0-9 @&$]*
- 0+ digits, letters, spaces,@
,&
or$
chars
$
- end of string.
Regular expression to allow alphanumeric with space and disallow certain special characters(<>&;)
The following regex matches everything except <>&;
:
const regex = /^[^<>&;]*$/
console.log(regex.test('Abc@123'))console.log(regex.test('Abc<123'))
Regex to validate an alphanumeric string with optional space
(?=^.{6,7}$)^(([0-9] ?){4}( ?[a-zA-Z]){2})$
will match
- 1111 ZZ
- 111 1ZZ
- 1 111ZZ
- 1111ZZ
- 1111 ZZ
but not
- 9999 1A
- 11111 Z
- 1111111
- 11 11 ZZ
https://regex101.com/r/lByOx6/1
EDIT: explanation
The "Positive Lookahead" part:
(?=^.{6,7}$)
this only matches if the string meets the requirements, BUT it does not 'consume' the characters.- . is any character
- {6,7} is about repetitions
so (?=^.{6,7}$)
is matched if the string has 6 or 7 characters, no matter what
Then the following part already 'consumes' the string to say that I want at the start 4 repetitions of numbers and optionally space, and at the end 2 repetitions of letters and optionally space. The second part would accept strings such as 1 1 1 1 Z Z
but as those are more than 7 characters, the first part wouldn't let the string match.
Regex for alphanumeric and single spaces
You can use this regex which uses a negative look ahead to discard the string that starts with a digit, and otherwise captures the text, which can be only separated with one space only,
^(?!\d)[a-zA-Z\d]+(?: [a-zA-Z\d]+)*$
Explanation of regex:
^
- Start of string(?!\d)
- Rejects the match if the string starts with a number[a-zA-Z\d]+
- Captures one or more alphanumeric characters(?: [a-zA-Z\d]+)*
- Further captures zero or more single space separated alphanumeric characters$
- End of string
Demo
Allow only alphanumeric characters or empty spaces with Regex
It is because part after |
doesn't have assertions of anchors ^
and $
.
You can use:
^$|^[a-zA-Z0-9]+$
Or:
^[a-zA-Z0-9]*$
Or:
^(?:[a-zA-Z0-9]+)?$
They will all be doing same thing.
Related Topics
How to Reference Assemblies Using Visual Studio Code
How to Convert Any Date Format to Yyyy-Mm-Dd
Post Byte Array to Web API Server Using Httpclient
How to Call Another Controller Action from a Controller in MVC
How to Read Request Body in an ASP.NET Core Webapi Controller
Adding an Incremental Number to Duplicate String
Using Linq to Remove Elements from a List<T>
Check If Datetime Is a Weekend or a Weekday
Empty String Not Being Converted to Null When Passing Json Object to Controller
Unexpected Character Encountered While Parsing Value
Calling a Function from Another Class in C#/.Net MVC App
How to Check If a Datetime Value Is Empty or Not in a Put Request
Client Specific Role Based Authentication
How to Secure the Asp.Net_Sessionid Cookie
Linq: Select an Object and Change Some Properties Without Creating a New Object