Matching a space in regex
If you're looking for a space, that would be " "
(one space).
If you're looking for one or more, it's " *"
(that's two spaces and an asterisk) or " +"
(one space and a plus).
If you're looking for common spacing, use "[ X]"
or "[ X][ X]*"
or "[ X]+"
where X
is the physical tab character (and each is preceded by a single space in all those examples).
These will work in every* regex engine I've ever seen (some of which don't even have the one-or-more "+"
character, ugh).
If you know you'll be using one of the more modern regex engines, "\s"
and its variations are the way to go. In addition, I believe word boundaries match start and end of lines as well, important when you're looking for words that may appear without preceding or following spaces.
For PHP specifically, this page may help.
From your edit, it appears you want to remove all non valid characters The start of this is (note the space inside the regex):
$newtag = preg_replace ("/[^a-zA-Z0-9 ]/", "", $tag);
# ^ space here
If you also want trickery to ensure there's only one space between each word and none at the start or end, that's a little more complicated (and probably another question) but the basic idea would be:
$newtag = preg_replace ("/ +/", " ", $tag); # convert all multispaces to space
$newtag = preg_replace ("/^ /", "", $tag); # remove space from start
$newtag = preg_replace ("/ $/", "", $tag); # and end
Simple Regular Expression for spaces and uppercase and lowercase letters only
The * in a regex means 0 or an unlimited amount. It will not allow other characters in your regex [a-zA-Z ]*
, but it will allow enmpty strings.
To require at least one character of upper case, lower case, space, but no numbers or symbols, you can use [a-zA-Z ]+
--
Here is a site that helps explain and text regular expressions. You'll see here that there are some subtle changes based on programming languages.
https://regex101.com/
Here is a site that systematically teaches them.
https://regexone.com/
Regular expression for a name that can contain not more than one space character
The pattern ^(?![\s]+$)(?![\s]{2,})[a-zA-Z\s]{2,25}$
that you tried matches:
- Assert that the string does not consist of 1 or more whitespace chars
^(?![\s]+$)
- Asserts not 2 or more whitespace chars at the beginning
(?![\s]{2,})
- Match 2-25 chars being either a-zA-Z or a whitespace char
[a-zA-Z\s]{2,25}$
There is no restriction to match a single space
in the whole string
Note that \s
could also match a newline or a tab.
What you could do is assert 2-25 characters in the string.
Then match 1+ chars a-zA-Z and optionally match a single space and 1+ chars a-zA-Z
^(?=.{2,25}$)[a-zA-Z]+(?: [a-zA-Z]+)?$
The pattern matches:
^
Start of string(?=.{2,25}$)
Positive lookahead, assert 2-25 chars in the string[a-zA-Z]+
Match 1+ chars A-Za-z(?: [a-zA-Z]+)?
Optionally match$
End of string
See a regex demo.
Related Topics
Moment Js Get First and Last Day of Current Month
How to Save .Xlsx Data to File as a Blob
Angular: Toggle Text of Button Based on Boolean Value in Model
Opening Pdf String in New Window With JavaScript
Javascript on Click Event for Multiple Buttons With Same Class
How to Hide the Parent Div If All Its Child Div Are Hidden
How to Prevent Form to Reload Page After Onsubmit
How to Use Document.Getelementbyid() Method in Typescript
Filter Json Object Array on Multiple Values or Arguments JavaScript
How to Remove Errors Warnings from Console
Instead of Downloading, Files Are Opening in Browser
How to Add Node Module to Angular Project If Is Not Schematics Enabled
Check If Image Exists on Server Using JavaScript
Ag-Grid Cellrender With Button Click
How to Disable Onclick Without Altering Its Function