Table of Contents
Syntax
Algorithm
Example 5
Finding digits inside the bracket
Replace digits inside the bracket
Home Web Front-end HTML Tutorial Find numbers in brackets in JavaScript's RegExp?

Find numbers in brackets in JavaScript's RegExp?

Aug 20, 2023 pm 06:45 PM
javascript regexp Find

Find numbers in brackets in JavaScripts RegExp?

In this tutorial, we learn how to find the number in brackets using JavaScript RegExp. Numbers (0-9) have ASCII values ​​from 48 to 57. We use [0-9] to represent the numbers in brackets in regular expressions. To find numbers in a range except all numbers, we can write a specific range. For example, to find a number between 4 and 8, we can write [4-8] in the regular expression pattern. Now, our goal is to find numbers inside brackets in text using RegExp in JavaScript. We can follow the following syntax to find the number in brackets.

Syntax

The following is the syntax of RegExp group [0-9] characters -

1

new RegExp("[0-9]") or simply /[0-9]/

Copy after login

/[0-9]/, is introduced in ES1. It is fully supported by all browsers. Like, Chrome, IE, Safari, Opera, FireFox and Edge.

RegExp has modifiers, such as g, i, m. "g" is used to perform global matching, "i" is used to perform case-insensitive matching, and "m" is used to perform multi-line matching.

Syntax for /[0-9]/ with modifier like

1

new RegExp("[0-9]", "g") or simply /[0-9]/g

Copy after login

Algorithm

  • Step 1 − Define a string containing some numbers.
  • STEP 2 − Define the RegExp pattern for digits between brackets.
  • Step 3 - Apply the match(pattern) function on the string defined above to find the numbers between brackets in the string.
  • Step 4 - Display the results - matching numbers.

Let's see some program examples for a clearer understanding.

Example 1

In the program below, we use string match(pattern) to find digits between 1 and 4 in the given string. We use RegExp pattern as /[1-4]/g. The string match() method returns an array of digits in the string.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!DOCTYPE html>

<html>

<body>

   <h2 id="Finding-digits-inside-the-bracket">Finding digits inside the bracket</h2>

   <p id = "text"></p>

   <p>Digits inside the bracket [1-4] :

   <span id= "result"></span>

   </p>

   <script>

      let myStr = "0127845639Hello";

      document.getElementById("text").innerHTML = myStr;

      let pattern = /[1-4]/g;

      let result = myStr.match(pattern);

      document.getElementById("result").innerHTML = result;

   </script>

</body>

</html>

Copy after login

Here, text is given as 0-9 digits and Hello word. In the pattern, we have given [1-4] only. match() method will search digits from 1 to 4 only. If mentioned digits found in the text, match() method will return an array of existing digits otherwise it will return as null. Let's see another example.

Example 2

In the program below, we take a string with no digits and try to find digits in the string. We use string match(pattern) to find digits between 1 and 4 in the given string. We use the RegExp pattern as / [1-4]/g. See what our output looks like.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!DOCTYPE html>

<html>

<body>

   <h1 id="Finding-digits-inside-the-bracket">Finding digits inside the bracket</h1>

   <p id= "result"></p>

   <script>

      let text = "567890";

      let pattern = /[1-4]/g;

      let result = text.match(pattern);

      if(result == null){

         document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets";

      } else {

         ocument.getElementById("result").innerHTML = result;

      }

   </script>

</body>

</html>

Copy after login

Here, we can observe in the pattern we have mentioned [1-4] but in the text we are given from 5-9 and 0. match() method will return as null because there are no findings. So, if the statement is executed. If input text is given as the first example, then match() will return an array of existing digits and another statement will be executed. Like,

Example 3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<!DOCTYPE html>

<html>

<body>

   <h1 id="Finding-digits-inside-the-bracket">Finding digits inside the bracket</h1>

   <p id= "result"></p>

   <script>

      let text = "0127845639Hello";

      let pattern = /[1-4]/g;

      let result = text.match(pattern);

      if(result == null){

         document.getElementById("result").innerHTML = "Sorry, there is no digits in text that mentioned in the brackets";

      } else {

         document.getElementById("result").innerHTML = "Digit(s) inside the inside the bracket: " + result;

      }

   </script>

</body>

</html>

Copy after login

Now, We will check how to replace word character(s) in a given text. Let’s see an example

Example 4

Find and replace numbers between brackets

In the following example, we use the split() and join() methods to find and replace the numbers between 1 and 4 with space characters.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!DOCTYPE html>

<html>

<body>

   <h1 id="Replace-digits-inside-the-bracket">Replace digits inside the bracket</h1>

   <p>After replacing the digits inside the bracket :

   <span id= "result"></span>

   </p>

   <script>

      let text = "0127845639Hello";

      let pattern = /[1-4]/g;

      let result = text.split(pattern).join(" ");

      document.getElementById("result").innerHTML = result;

   </script>

</body>

</html>

Copy after login

Example 5

We will also check to replace the digits inside the bracket using a simpler way. Like,

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<!DOCTYPE html>

<html>

<body>

   <h1 id="Replace-digits-inside-the-bracket">Replace digits inside the bracket</h1>

   <p>After replacing the digits inside the bracket :

   <span id= "result"></span>

   </p>

   <script>

      let text = "0127845639Hello";

      let pattern = /[1-4]/g;

      let result = text.replace(pattern , " ");

      document.getElementById("result").innerHTML = result;

   </script>

</body>

</html>

Copy after login

As we discussed, g for global matches. Instead of stopping with the first occurrence, it will look for all the occurrences.

Hope this tutorial will give knowledge on how to find digits inside the brackets using RegExp in JavaScript.

The above is the detailed content of Find numbers in brackets in JavaScript's RegExp?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

4 Ways to Turn Off Find My on iPhone 4 Ways to Turn Off Find My on iPhone Feb 02, 2024 pm 04:15 PM

Apple's Find My app allows you to locate your iPhone or other device to prevent it from being lost or forgotten. While Find My is a useful tool for tracking devices, you may want to disable it if you're concerned about privacy issues, don't want to drain your battery, or for other reasons. Fortunately, there are several ways to turn off Find My on iPhone, all of which we will explain in this article. How to Turn off Find My on iPhone [4 Methods] You can turn off Find My on iPhone in four ways. If you used Method 1 to turn off Find, you can do this from the device you want to disable it on. To proceed with methods 2, 3, and 4, the iPhone that you want to turn off Find Finder should be powered off or

How to check the hard disk serial number and mac address How to check the hard disk serial number and mac address Feb 18, 2024 pm 07:45 PM

Hard drive serial numbers and MAC addresses are important identifiers in computer hardware and are very useful in managing and maintaining computer systems. This article will introduce how to find the hard disk serial number and MAC address. 1. Find the hard drive serial number. The hard drive serial number is a unique identifier used by the hard drive manufacturer to identify and track the hard drive. In different operating systems, the method of finding the hard drive serial number is slightly different. Windows: Open Command Prompt (search for "cmd" in the Start menu) and enter the following command and press Enter: wmicdisk

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

See all articles