javascript - JavaScript 开始使用正则表达式和偏移量
问题描述
我正在做一些字符串解析并想使用正则表达式。我正在遍历字符串,并希望使用正则表达式和偏移量应用类似“startsWith”的内容,如果找到则返回匹配项,否则返回 null。在伪javascript中:
function startsWith(string, regex, offset) {
if (regex_matches_at_offset) {
return match;
} else {
return null;
}
}
一个简单直接的解决方案是首先应用子字符串然后匹配。但我想要使用正则表达式的“startsWith”之类的东西。
如果它是字符串而不是正则表达式,我会选择startsWith
function startsWith(string, other_string, offset) {
let starts_with=s.startsWith(other_string, offset); // startsWith from position offset onwards, as other_string has fixed length the "match" is also known
if (starts_with) {
return other_string; // other_string = match
} else {
return null;
}
}
但是对于正则表达式,我当前的解决方案(仅用于测试目的)看起来像:
function startsWith(string, regex, offset) {
let end_part=s.substring(offset); // Substring, performance issue
let match=end_part.match(regex); // Match it as we need the match
if (match.length === 0) {
return null;
} else {
match=match[0]; // Only care about 1st match
}
if (end_part.startsWith(match)) { // Check if match already starts at first position
return match;
} else {
return null;
}
}
这不是很令人满意,因为存在明显的问题(复制大部分字符串,执行正则表达式搜索两次......
预期的 :
- 从给定位置开始在字符串中查找正则表达式,不仅是布尔值,而且还匹配
- 高性能(例如,不要复制整个字符串)
例子 :
startsWith("Hello world !", /h/i, 0); // "H"
startsWith("Hello world !", /wor?/i, 6); // "wor"
startsWith("Hello world !", /wor?/i, 10); // null
startsWith("Hello world !", /z/i, 0); // null
解决方案
一种方法是基于作为字符串传递的偏移量和模式构建正则表达式
- 这里正则表达式的初始部分
.
用于匹配任何字符到偏移量 - 第二次在捕获的组中使用传递的模式只返回所需的匹配
let startsWith = (str, reg, offset) =>{
let regex = `^.{${0,offset}}(${reg})`
let final = new RegExp(regex,'i')
let found = str.match(final)
console.log(found ? found[1] : found)
}
startsWith("Hello world !", 'h', 0); // "H"
startsWith("Hello world !", 'wor?', 6); // "wor"
startsWith("Hello world !", 'wor?', 10); // null
startsWith("Hello world !", 'z', 0); // null
正如@mark所提到的,如果我们希望将正则表达式作为参数传递给函数,我们可以使用源属性
let startsWith = (str, reg, offset) =>{
let regex = `^.{${0,offset}}(${reg.source})`
let final = new RegExp(regex,'i')
let found = str.match(final)
console.log(found ? found[1] : found)
}
startsWith("Hello world !", /h/i, 0); // "H"
startsWith("Hello world !", /wor?/i, 6); // "wor"
startsWith("Hello world !", /wor?/i, 10); // null
startsWith("Hello world !", /z/i, 0); // null
推荐阅读
- amazon-web-services - 使用 AWS 安排对 IIS 的请求
- java - 确定字符是否为有效十六进制数字的方法?
- go - 如何让golang的Logrus通过多个文件共享同一个配置?
- java - 处理在 Java/Selenium 中停止处理的浏览器弹出窗口
- javascript - 将 JQuery Each 转换为 ES6/7 循环
- c# - C#如何为不同的图像大小获取相同的坐标
- python - 将数据框折叠到 NaN 值上
- c# - Firebase 云消息传递和 Azure 通知中心错误
- sql - 在不考虑实际天数的情况下查找中间的月份
- json - Json schema validating length of one field based on another field