
在本教程中,我们学习如何使用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”用于执行多行匹配。
立即学习“Java免费学习笔记(深入)”;
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.
Finding digits inside the bracket
Digits inside the bracket [1-4] :
let myStr = "0127845639Hello"; document.getElementById("text").innerHTML = myStr; let pattern = /[1-4]/g; let result = myStr.match(pattern); document.getElementById("result").innerHTML = result;
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.
Finding digits inside the bracket
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; }
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
Finding digits inside the bracket
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; }
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之间的数字为空格字符。
Replace digits inside the bracket
After replacing the digits inside the bracket :
let text = "0127845639Hello"; let pattern = /[1-4]/g; let result = text.split(pattern).join(" "); document.getElementById("result").innerHTML = result;
Example 5
We will also check to replace the digits inside the bracket using a simpler way. Like,
Replace digits inside the bracket
After replacing the digits inside the bracket :
let text = "0127845639Hello"; let pattern = /[1-4]/g; let result = text.replace(pattern , " "); document.getElementById("result").innerHTML = result;
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中查找括号中的数字?的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1551081.html
微信扫一扫
支付宝扫一扫