在本教程中,我们学习如何使用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中文网其他相关文章!

布尔属性是HTML中的特殊属性,不需要值即可激活。1.布尔属性通过存在与否控制元素行为,如disabled禁用输入框。2.它们的工作原理是浏览器解析时根据属性的存在改变元素行为。3.基本用法是直接添加属性,高级用法可通过JavaScript动态控制。4.常见错误是误以为需要设置值,正确写法应简洁。5.最佳实践是保持代码简洁,合理使用布尔属性以优化网页性能和用户体验。

HTML代码可以通过在线验证器、集成工具和自动化流程来确保其清洁度。1)使用W3CMarkupValidationService在线验证HTML代码。2)在VisualStudioCode中安装并配置HTMLHint扩展进行实时验证。3)利用HTMLTidy在构建流程中自动验证和清理HTML文件。

HTML、CSS和JavaScript是构建现代网页的核心技术:1.HTML定义网页结构,2.CSS负责网页外观,3.JavaScript提供网页动态和交互性,它们共同作用,打造出用户体验良好的网站。

HTML的功能是定义网页的结构和内容,其目的在于提供一种标准化的方式来展示信息。1)HTML通过标签和属性组织网页的各个部分,如标题和段落。2)它支持内容与表现分离,提升维护效率。3)HTML具有可扩展性,允许自定义标签增强SEO。

HTML的未来趋势是语义化和Web组件,CSS的未来趋势是CSS-in-JS和CSSHoudini,JavaScript的未来趋势是WebAssembly和Serverless。1.HTML的语义化提高可访问性和SEO效果,Web组件提升开发效率但需注意浏览器兼容性。2.CSS-in-JS增强样式管理灵活性但可能增大文件体积,CSSHoudini允许直接操作CSS渲染。3.WebAssembly优化浏览器应用性能但学习曲线陡,Serverless简化开发但需优化冷启动问题。

HTML、CSS和JavaScript在Web开发中的作用分别是:1.HTML定义网页结构,2.CSS控制网页样式,3.JavaScript添加动态行为。它们共同构建了现代网站的框架、美观和交互性。

HTML的未来充满了无限可能。1)新功能和标准将包括更多的语义化标签和WebComponents的普及。2)网页设计趋势将继续向响应式和无障碍设计发展。3)性能优化将通过响应式图片加载和延迟加载技术提升用户体验。

HTML、CSS和JavaScript在网页开发中的角色分别是:HTML负责内容结构,CSS负责样式,JavaScript负责动态行为。1.HTML通过标签定义网页结构和内容,确保语义化。2.CSS通过选择器和属性控制网页样式,使其美观易读。3.JavaScript通过脚本控制网页行为,实现动态和交互功能。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

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

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

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器