在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 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
在括号之间查找和替换数字
在下面的示例中,我们使用split()和join()方法找到并替换1和4之间的数字为空格字符。
<!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.
以上是在JavaScript的RegExp中查找括号中的数字?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

如何使用WebSocket和JavaScript实现在线语音识别系统引言:随着科技的不断发展,语音识别技术已经成为了人工智能领域的重要组成部分。而基于WebSocket和JavaScript实现的在线语音识别系统,具备了低延迟、实时性和跨平台的特点,成为了一种被广泛应用的解决方案。本文将介绍如何使用WebSocket和JavaScript来实现在线语音识别系

硬盘序列号和MAC地址是电脑硬件中重要的标识符,它们在管理和维护电脑系统时非常有用。本文将介绍如何查找硬盘序列号和MAC地址。一、查找硬盘序列号硬盘序列号是硬盘制造商为了识别和追踪硬盘的唯一标识符。在不同的操作系统中,查找硬盘序列号的方法略有不同。Windows系统:打开命令提示符(在开始菜单中搜索“cmd”),然后输入以下命令并按回车键:wmicdisk

WebSocket与JavaScript:实现实时监控系统的关键技术引言:随着互联网技术的快速发展,实时监控系统在各个领域中得到了广泛的应用。而实现实时监控的关键技术之一就是WebSocket与JavaScript的结合使用。本文将介绍WebSocket与JavaScript在实时监控系统中的应用,并给出代码示例,详细解释其实现原理。一、WebSocket技

如何利用JavaScript和WebSocket实现实时在线点餐系统介绍:随着互联网的普及和技术的进步,越来越多的餐厅开始提供在线点餐服务。为了实现实时在线点餐系统,我们可以利用JavaScript和WebSocket技术。WebSocket是一种基于TCP协议的全双工通信协议,可以实现客户端与服务器的实时双向通信。在实时在线点餐系统中,当用户选择菜品并下单

Apple的“查找”应用程序允许您定位您的iPhone或其他设备,以防止丢失或遗忘。虽然“查找”是一个有用的工具来追踪设备,但如果您关注隐私问题、不想耗尽电池或其他原因,您可能希望禁用它。幸运的是,有几种方法可以关闭iPhone上的“查找”,我们将在这篇文章中解释所有这些方法。如何在iPhone上关闭“查找”[4种方法]您可以通过四种方式关闭iPhone的“查找”。如果您使用方法1关闭“查找”,则可以从要禁用它的设备上执行此操作。要继续执行方法2、3和4,要关闭“查找”的iPhone应关闭电源或

如何使用WebSocket和JavaScript实现在线预约系统在当今数字化的时代,越来越多的业务和服务都需要提供在线预约功能。而实现一个高效、实时的在线预约系统是至关重要的。本文将介绍如何使用WebSocket和JavaScript来实现一个在线预约系统,并提供具体的代码示例。一、什么是WebSocketWebSocket是一种在单个TCP连接上进行全双工

JavaScript和WebSocket:打造高效的实时天气预报系统引言:如今,天气预报的准确性对于日常生活以及决策制定具有重要意义。随着技术的发展,我们可以通过实时获取天气数据来提供更准确可靠的天气预报。在本文中,我们将学习如何使用JavaScript和WebSocket技术,来构建一个高效的实时天气预报系统。本文将通过具体的代码示例来展示实现的过程。We

JavaScript教程:如何获取HTTP状态码,需要具体代码示例前言:在Web开发中,经常会涉及到与服务器进行数据交互的场景。在与服务器进行通信时,我们经常需要获取返回的HTTP状态码来判断操作是否成功,根据不同的状态码来进行相应的处理。本篇文章将教你如何使用JavaScript获取HTTP状态码,并提供一些实用的代码示例。使用XMLHttpRequest
