Check If String Contains Only Latin Letters In JavaScript

Check if String contains only Latin Letters in JavaScript

Hello everyone. To continue the topic of working with strings in JavaScript, today we will discuss about how to check if string contains only Latin letters in JavaScript with methods: using test() and using exec().

How to check if string contains only Latin letters in JavaScript

Below are two simple ways we want to introduce to you. These ways work well, even on old browser versions.

Using test()

You can read its syntax and usage here. The knowledge of regex (regular expression) is also essential to tackle this problem. If you don’t know about it, please visit here to study before continuing. The regex we will use to check is: /^[a-zA-Z]+$/, which means all strings that contain letters from a to z or A to Z (they are called latin letters).

First, we use the method test() on the regex we have declared and get the result of whether the test String matches the pattern. If yes, the function test() will return true. Otherwise, it will return false.

// Declare the string to test
let string = "LearnShareIT"

// Check if string contains only Latin letters
console.log(/^[a-zA-Z]+$/.test(string))

Output:

true

Another example:

// Declare the string to test
let string = "Learn Share IT"

// Check if string contains only Latin letters
console.log(/^[a-zA-Z]+$/.test(string))

Output:

false

As you can see, in the first example, the string to test has no spaces and contains letters in the English alphabet, so it is correct. However, the second example has a whitespace string; so they are not latin letters, we receive the false value.

Using exec()

Syntax:

RegExpObject.exec(string)

Parameter:

  • string: the string to test the regex.

The exec() method checks for a regex in the string. If the pattern appears in the string, an array will be returned. Otherwise, it will return null. The use of it is somehow similar to the test() method:

// Declare the string to test
let string = "LearnShareIT"

// Check if string contains only Latin letters
console.log(/^[a-zA-Z]+$/.exec(string)!=null)

Output:

true

Another example:

// Declare the string to test
let string = "Learn Share IT"

// Check if string contains only Latin letters
console.log(/^[a-zA-Z]+$/.exec(string)!=null)

Output:

false

As we have explained, this function is similar to the test() function. If the provided string matches the regex we declared, it will return a non-null object, an array. Otherwise, it will return null instead. Therefore, as we want to check if the string contains only Latin letters, we need to call the exec() method on the regex /^[a-zA-Z]+$/ and check whether the method differs from null or not. The output is the answer we are expecting for our question.

Summary

In this tutorial, we have introduced two ways to check if string contains only Latin letters in JavaScript. We suggest you use the first approach: using test(), which always returns a boolean value. If you have a better solution, please share it with us through the reply. Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *