search
HomeWeb Front-endHTML TutorialFind numbers in brackets in JavaScript's RegExp?

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 -

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

/[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

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

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.

<!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>

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.

<!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>

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

<!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>

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.

<!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>

Example 5

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

<!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>

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
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment