正则表达式文字是正则表达式,是与字符串匹配的字符序列。有时,开发人员需要组合正则表达式。然而,正则表达式也是字符串的一种类型,但我们不能像字符串一样使用“+”运算符将它们连接起来。
因此,我们首先需要从两个正则表达式中获取标志。之后,我们必须过滤所有唯一的标志,并在组合多个正则表达式文字后创建一个新的正则表达式。
语法
用户可以按照以下语法在 JavaScript 中连接正则表达式文字。
let allFlags = regex1.flags + regex2.flags;
let uniqueFlags = new Set(allFlags.split(''));
allFlags = [...uniqueFlags].join('');
let combined = new RegExp(regex1.source + regex2.source, allFlags);
在上面的语法中,首先,我们获得两个正则表达式的标志。之后,我们从中创建一个集合来获取唯一标志并组合两个正则表达式文字。
示例 1
在下面的示例中,我们定义了两个正则表达式文字。我们使用正则表达式文字的“flags”属性从两个正则表达式中获取标志。之后,我们创建了一组标志以获得独特的标志。接下来,我们将集合转换为数组。
之后,我们使用正则表达式的“source”属性来获取正则表达式文字,并使用“+”运算符组合两个正则表达式文字。此外,我们在创建组合的正则表达式文字时使用了两个正则表达式文字中包含的所有唯一标志。
<html> <body> <h2>Concatenating the <i> Regex literals </i> in JavaScript</h2> <div id = "output"> </div> <script> let output = document.getElementById('output'); // concatenate two regex literals let regex1 = /hello/i; let regex2 = /world/g; let allFlags = regex1.flags + regex2.flags; let uniqueFlags = new Set(allFlags.split('')); allFlags = [...uniqueFlags].join(''); let combined = new RegExp(regex1.source + regex2.source, allFlags); output.innerHTML += "The first regex is: " + regex1 + "<br>"; output.innerHTML += "The second regex is: " + regex2 + "<br>"; output.innerHTML += "The combined regex is: " + combined + "<br>"; </script> </body> </html>
示例 2
在下面的示例中,用户需要输入正则表达式文字和相关标志。此外,用户需要填写字符串输入以使用组合正则表达式进行测试。
每当用户按下按钮时,它就会执行combineRegex()函数。其中我们将两个输入正则表达式与适当的标志结合起来。之后,我们使用 test() 方法检查字符串是否与组合的正则表达式匹配,并返回布尔值。
<html>
<body>
<h2>Concatenating the <i> Regex literals </i> in JavaScript</h2>
<input type = "text" placeholder = "regex1" id = "regex1">
<input type = "text" placeholder = "flags" id = "flag1">
<br> <br>
<input type = "text" placeholder = "regex2" id = "regex2">
<input type = "text" placeholder = "flags" id = "flag2">
<br> <br>
<input type = "text" placeholder = "string to test regex" id = "str">
<br> <br>
<div id = "output"> </div>
<button id="btn" onclick="combineRegex()"> Combine and Match Regex </button>
<script>
let output = document.getElementById('output');
function combineRegex() {
let regex1 = new RegExp(document.getElementById('regex1').value, document.getElementById('flag1').value);
let regex2 = new RegExp(document.getElementById('regex2').value, document.getElementById('flag2').value);
let allFlags = regex1.flags + regex2.flags;
let uniqueFlags = new Set(allFlags.split(''));
allFlags = [...uniqueFlags].join('');
let combined = new RegExp(regex1.source + regex2.source, allFlags);
let str = document.getElementById('str').value;
let result = combined.test(str);
output.innerHTML += "The combined regex " + combined + " matches the string " + str + " : " + result;
}
</script>
</body>
</html>
示例 3
在下面的示例中,我们首先以正则字符串的形式编写正则表达式。之后,我们合并两个字符串并使用 RegExp() 构造函数从组合字符串创建一个新的正则表达式。
此外,我们可以将所需的标志作为第二个参数传递。
<html>
<body>
<h2>Concatenating the <i> Regex literals </i> in JavaScript</h2>
<div id = "output"> </div>
<script>
let output = document.getElementById('output');
let part1 = "Hello";
let part2 = " World";
var pattern = new RegExp(part1 + part2, "g");
output.innerHTML = "The combined regex is: " + pattern + "<br>";
</script>
</body>
</html>
结论
用户学会了在 JavaScript 中连接正则表达式文字。在第一个示例中,我们在创建正则表达式文字后将其连接起来。在上一个示例中,我们首先组合字符串,并使用组合字符串创建一个新的正则表达式。但是,最后一个示例不使用正则表达式文字,因为它使用构造函数创建正则表达式。
以上就是如何在JavaScript中连接正则表达式字面量?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 如何在JavaScript中连接正则表达式字面量?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?