Skip to content
the poor coder

regular expression start and end with same character javascript

Poor Man
Published
Reading time
2 min read
On this page

Regular Expression Start and End with Same Character JavaScript#

Regular expressions, also known as regex or regexp, are patterns used to match character combinations in strings. In JavaScript, we can create regular expressions using the RegExp constructor or by using a regex literal enclosed in forward slashes (/regex/).

If you want to match a string that starts and ends with the same character, you can use a backreference in your regular expression.

Method 1: Using a Backreference#

A backreference allows us to match a previously matched group. In this case, we want to match a string that starts and ends with the same character. We can use a capturing group to match the first character, then use a backreference to match the same character at the end of the string.

JavaScript

      const regex = /^([a-z])\w*\1$/;

      console.log(regex.test("anna")); // true
      console.log(regex.test("apple")); // false
    

In the above example, we are using the ^ and $ anchors to match the beginning and end of the string, respectively. The ([a-z]) capturing group matches any lowercase letter at the start of the string. The \w* quantifier matches zero or more word characters (letters, digits, or underscores). Finally, the \1 backreference matches the same character as the one matched by the first capturing group.

Method 2: Using Lookahead#

We can also use a positive lookahead to match a string that starts and ends with the same character. A positive lookahead is a non-capturing group that matches a pattern without including it in the overall match.

JavaScript

      const regex = /^(?=\w*(\w)\w*$)\1/;

      console.log(regex.test("anna")); // true
      console.log(regex.test("apple")); // false
    

In the above example, we are using the ^ anchor to match the beginning of the string. The positive lookahead (?=\w*(\w)\w*$) matches any number of word characters followed by a capturing group that matches any character (\w) at the end of the string. Finally, the \1 backreference matches the same character as the one matched by the first capturing group.

Wrapping Up#

Both of these methods will match strings that start and end with the same character. Which one you use will depend on your personal preference and the specific requirements of your project.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC