Skip to content
the poor coder

js regex match start and end of string

Poor Man
Published
Reading time
2 min read
On this page

JS Regex: Match Start and End of String#

Regular expressions, or regex, are a powerful and flexible tool for searching, matching, and manipulating text. They can be used in many programming languages, including JavaScript.

If you want to match the start or end of a string in JavaScript using regex, there are a few different ways to do it.

Match Start of String#

To match the start of a string using regex in JavaScript, you can use the caret (^) character. This character is called an anchor and represents the start of the string:

Text

let str = "Hello World";
let regex = /^Hello/;

if (regex.test(str)) {
  console.log("Match found at start of string");
}

In this example, we're testing whether the string "Hello World" starts with "Hello" using the regex /^Hello/. The test() method returns true because the regex matches the start of the string.

Match End of String#

To match the end of a string using regex in JavaScript, you can use the dollar sign ($) character. This character is also an anchor and represents the end of the string:

Text

let str = "Hello World";
let regex = /World$/;

if (regex.test(str)) {
  console.log("Match found at end of string");
}

In this example, we're testing whether the string "Hello World" ends with "World" using the regex /World$/. The test() method returns true because the regex matches the end of the string.

Match Start and End of String#

You can also match both the start and end of a string using regex in JavaScript. To do this, you can combine the caret (^) and dollar sign ($) characters:

Text

let str = "Hello World";
let regex = /^Hello.*World$/;

if (regex.test(str)) {
  console.log("Match found at start and end of string");
}

In this example, we're testing whether the string "Hello World" starts with "Hello" and ends with "World" using the regex /^Hello.*World$/. The test() method returns true because the regex matches the start and end of the string.

Overall, using regex to match the start and end of a string in JavaScript can be a useful technique for a variety of text processing tasks.

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