如何使用 JavaScript 正则表达式查找括号之间的数字?
在 JavaScript 中,正则表达式 [^0-9] 用于查找括号内的字符,但不是数字。除了括号中提到的数字外,它会将文本中剩余的字符作为数组返回。
我们都知道 JavaScript 中的 RegExp(正则表达式)。RegExp 是一个对象,它指定用于对字符串执行搜索和替换操作或用于输入验证的模式。RegExp 在 ES1 中引入,并得到所有浏览器的完全支持。
语法
非数字或 /[^0-9]/ 字符的语法为:
new RegExp("[^0-9]") or simply /[^0-9]/
这里 /[^0-9]/ 在 ES1 中引入。它得到所有浏览器的完全支持。例如,Chrome、IE、Safari、Opera、Firefox 和 Edge。
RegExp 具有修饰符,如 g、i、m。“g”用于执行全局匹配,“i”用于执行不区分大小写的匹配,“m”用于执行多行匹配。
带有修饰符的 \w 的语法,例如:
new RegExp("[^0-9]", "g") or simply /[^0-9]/g
查找非括号数字的步骤
步骤 1 - 定义一个包含数字的字符串。
步骤 2 - 定义非括号数字的正则表达式模式。
步骤 3 - 使用 match() 方法将字符串与正则表达式模式匹配。
步骤 4 - 显示匹配的数字。
现在,让我们看看如何查找括号内除了数字以外的字符。
示例
在此示例中,我们查找括号 [0, 4] 之外的数字。
<!DOCTYPE html>
<html>
<body>
<h2>Digits not between the bracket</h2>
<p>Digits not in the bracket [1, 4]:
<p id = "result"></p>
</p>
<script>
let text = "1234567890";
let pattern = /[^1-4]/g;
let result = text.match(pattern);
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>
示例
在下面的示例中,我们查找括号 [1, 4] 之外的字符。
<!DOCTYPE html>
<html>
<body>
<h2>RegExp [^1-4]</h2>
<p>Characters not in the bracket [1-4]:
<p id = "result"></p>
</p>
<script>
let text = "1234567890_Hello@$%^&65434320867042ecsceQW";
let pattern = /[^1-4]/g;
let result = text.match(pattern);
document.getElementById("result").innerHTML = result;
</script>
</body>
</html>
在这里,我们可以观察到我们给定的文本为 0-9 和 "_Hello@$%^&"。但在模式中,我们提到不要打印 [1-4] 中的数字。因此,match() 方法会将剩余的字符作为数组返回,否则 match() 方法会返回 null。让我们看另一个例子
示例
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p id = "result"></p>
<script>
let text = "123456789";
let pattern = /[^1-9]/g;
let result = text.match(pattern);
if(result == null){
document.getElementById("result").innerHTML = "No characters found";
} else {
document.getElementById("result").innerHTML = "Characters are " + result;
}
</script>
</body>
</html>
这里,文本为 1-9,模式也不为 1-9。因此,match() 方法不会在文本中找到除 1-9 之外的任何字符。因此,它将返回 null。然后,如果语句被执行。如果我们给出如第一个示例所示的文本,则将执行 else 分支。
示例
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p id = "result">Characters not between the bracket [1, 9]: <br><br> </p>
<script>
let text = "9876543210_Hello@$%^&";
let pattern = /[^1-9]/g;
let result = text.match(pattern);
if(result == null){
document.getElementById("result").innerHTML += "No characters found";
} else {
document.getElementById("result").innerHTML += "Characters are " + result;
}
</script>
</body>
</html>
现在,我们将检查如何替换给定文本中的单词字符。为此,我们将使用另一个仅包含数字的正则表达式。我们将此正则表达式与先前的结果匹配。让我们看一个例子
示例
在下面的示例中,我们仅查找括号 [1, 4] 之外的数字。
<!DOCTYPE html>
<html>
<body>
<h1>RegExp [^0-9]</h1>
<p>Digits not in the bracket [1,4]:
<p id = "result"></p>
</p>
<script>
let text = "1234567890_Hello@$%^&65434320867042ecsceQW";
let pattern = /[^1-4]/g;
let result = text.match(pattern);
let pattern2 = /\d/g;
document.getElementById("result").innerHTML =
JSON.stringify(result).match(pattern2);
</script>
</body>
</html>
在上面的程序中,我们首先查找括号 [1, 4] 之外的所有字符,然后从这组字符中仅查找数字。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP