Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex for alphabetic characters and spaces? [closed]

I need a regex for javascript that includes a-z, A-Z, and spaces

For example, the string "Bob says Hi" would be accepted, but not "There were 4 clowns"

The closest I've gotten is /^[a-zA-Z]+$/ which includes a-z and A-Z but not spaces.

like image 445
DontTurnAround Avatar asked Nov 09 '11 01:11

DontTurnAround


People also ask

What does \f mean in regex?

\f stands for form feed, which is a special character used to instruct the printer to start a new page.

What is \d in JavaScript regex?

The RegExp \D Metacharacter in JavaScript is used to search non digit characters i.e all the characters except digits. It is same as [^0-9]. Example 1: This example searches the non-digit characters in the whole string.

How do you stop space in regex?

You can easily trim unnecessary whitespace from the start and the end of a string or the lines in a text file by doing a regex search-and-replace. Search for ^[ \t]+ and replace with nothing to delete leading whitespace (spaces and tabs). Search for [ \t]+$ to trim trailing whitespace.

What is \b in regex JavaScript?

The RegExp \B Metacharacter in JavaScript is used to find a match which is not present at the beginning or end of a word. If a match is found it returns the word else it returns NULL. Example 1: This example matches the word “for” which is not present at the beginning or end of the word.

How do I check for alphabetic characters in regular expressions?

A regular expression for alphabetic characters should check that the expression contains one or more letters of the alphabet in either upper (A to Z) or lower case (a to z), and does not contain any other characters such as numbers or special characters. /^ [A-Za-z]+$/.

How to allow only alphanumeric characters in JavaScript?

You can use a RegEx expression that matches all the characters except a number or an alphabet in JavaScript. You will use the given regular expression to validate user input to allow only alphanumeric characters.

How do you match a space in a regular expression?

The whitespace character (\s) can also be used to match a space, although it will also match other whitespace characters like tabs, line feeds, and carriage returns. The alphabetic regular expressions shown above will match both upper and lowercase characters or a mix of the two.

Is there a way to use named character sets in regex?

Unfortunately, the regex engine in most browsers does not support named character sets, but there are some libraries to help with that. For languages/platforms that do support named character sets, you can use /^ [\p {Letter}\d\_\-\s]+$/ Show activity on this post. Show activity on this post.


1 Answers

/^[A-Za-z ]+$/ or /^[A-Za-z\s]+$/

More good stuff here:
http://www.regular-expressions.info/javascript.html


or just /\w+$/ if you also want 0-9 and underscores (\w stands for "word character", usually [A-Za-z0-9_]). But your recent edit indicates that you dont want 0-9, so use one of the first 2 above.

like image 136
chown Avatar answered Oct 09 '22 10:10

chown