在本教程中,我们学习如何使用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中查找括号中的数字?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 在JavaScript的RegExp中查找括号中的数字?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?