Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latin charcters included in JavaScript Regex

I'm looking to included the Latin characters below in a JavaScript regex for a string validation.

ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏàáâãäåæçèéêëìíîïÐÑÒÓÔÕÖØÙÚÛÜÝÞßðñòóôõöøùúûüýþÿ

So far I just have a basic string regex.

var stringReg = /[a-zA-Z\-\'\ ]/i;

Thanks in advance. Smccullough

like image 684
Smccullough Avatar asked Aug 31 '11 14:08

Smccullough


People also ask

What regex does JavaScript use?

Using regular expressions in JavaScript. Regular expressions are used with the RegExp methods test() and exec() and with the String methods match() , replace() , search() , and split() . Executes a search for a match in a string. It returns an array of information or null on a mismatch.

How do you denote special characters in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What are character classes in regex?

In the context of regular expressions, a character class is a set of characters enclosed within square brackets. It specifies the characters that will successfully match a single character from a given input string.

Does JavaScript support regex?

Regular expressions are patterns that provide a powerful way to search and replace in text. In JavaScript, they are available via the RegExp object, as well as being integrated in methods of strings.


2 Answers

Try this:

/[A-Za-z\u00C0-\u00D6\u00D8-\u00f6\u00f8-\u00ff]+/g
like image 168
Serhii Smirnov Avatar answered Sep 24 '22 03:09

Serhii Smirnov


Below java script function will work fine, you can add number as well.

function myFunction() {
  var str = "xq234"; 
  var allowChars = "^[a-zA-ZÀ-ÿ]+$";
  var res = str.match(allowChars);
  if(!str.match(allowChars)){
    res="true";
  }
  else {
    res="false";
  }
  document.getElementById("demo").innerHTML = res;`enter code here`
like image 29
Tapan Upadhyay Avatar answered Sep 20 '22 03:09

Tapan Upadhyay