首页 >web前端 >html教程 >在JavaScript的RegExp中查找括号中的数字?

在JavaScript的RegExp中查找括号中的数字?

WBOY
WBOY转载
2023-08-20 18:45:45767浏览

在JavaScript的RegExp中查找括号中的数字?

在本教程中,我们学习如何使用JavaScript RegExp找到括号中的数字。数字(0-9)的ASCII值从48到57。我们在正则表达式中用[0-9]表示括号中的数字。要找到除所有数字之外的范围内的数字,我们可以写出特定的范围。例如,要找到4和8之间的数字,我们可以在正则表达式模式中写为[4-8]。现在,我们的目标是使用JavaScript中的RegExp在文本中找到括号内的数字。我们可以按照以下语法来找到括号中的数字。

Syntax

以下是RegExp组[0-9]字符的语法 -

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有修饰符,如g,i,m。"g"用于执行全局匹配,"i"用于执行不区分大小写的匹配,"m"用于执行多行匹配。

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

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

Algorithm

  • 步骤 1 − 定义一个包含一些数字的字符串。
  • STEP 2 − Define the RegExp pattern for digits between brackets.
  • 步骤 3 - 在上述定义的字符串上应用match(pattern)函数,以在字符串中查找括号之间的数字。
  • 第四步 - 显示结果- 匹配的数字。

让我们看一些程序示例,以便更清楚地理解。

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

在括号之间查找和替换数字

在下面的示例中,我们使用split()和join()方法找到并替换1和4之间的数字为空格字符。

<!DOCTYPE html>
<html>
<body>
   <h1>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>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.

以上是在JavaScript的RegExp中查找括号中的数字?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除