比方说
let sentence= "Dear user {#val#}{#val#} thanks"{#val#}是上述语句中的动态值。这里可以有任何值来代替{#val#},但最少可以有0个字符,最多可以有5个字符。因此,我将{#val#}替换为.{0,5}。除了{#val#}部分之外,我不需要考虑空格,所以我形成的正则表达式应该是,
let regex = /^Dear\s*user\s*.{0,5}.{0,5} thanks$/i
let customermsg = "Dear user 1 2 thanks" //Should be valid
let customermsg1 = "Dear user 12345 6789 thanks" //Should be valid
let customermsg2 = "Dear user 123 5 6789 thanks" //Should be valid because space can also be considered as a character and for fist .{0,5} => 123 5 and for second .{0,5} => 6789
let customermsg3 = "Dear user 1 thanks" //Should pass
let customermsg4 = "Dea r user 1 tha nks" // Should Pass since spaces are not considered in the static portion.但是当我尝试使用下面的代码进行测试时,
regex.test(customermsg)完全相反。就连我也试过下面的方法。
let splitters=/{\\#val\\#}|((\\s\*))/gi;
sentence = sentence.replace(splitters, (x, y) => y ? y : ".(\\S{0,5})"); 这会将正则表达式返回为
/^Dear\s*user\s*.(\S{0,5}).(\S{0,5})\s*thanks$/但这也没有像预期的那样起作用。我被困在这里了。请帮帮我。
发布于 2021-02-24 18:56:06
你需要做的是检查数字和空格是否多次出现
“亲爱的用户1谢谢”//应该失败,因为只有一个值
因此,您的正则表达式很好,只是它不会检查一个数字和空格是否出现多次。
使用以下正则表达式
Dear\s*user\s*([\d]+[\s]+){2,5}\s*thanks$([\d]+[\s]+){2,5}\s*- Capturing group matching _digit_ between zero and unlimited times and _white space_ between zero and unlimited times, for atleast two times and max five.([\d]+[\s]+){2,5}\s*部分确保一个数字至少出现两次,因此字符串Dear user ... thanks中的单个数字将失败。
您可以在数字之前、之间和之后使用任意多的空格。
let regex = /Dear\s*user\s*([\d]+[\s]+){2,5}\s*thanks$/i
let customermsg = "Dear user 1 2 thanks" //Should be valid
let customermsg1 = "Dear user 12345 6789 thanks" //Should be valid
let customermsg2 = "Dear user 123 5 6789 thanks" //Should be valid because space can also be considered as a character and for fist .{1,5} => 123 5 and for second .{1,5} => 6789
let customermsg3 = "Dear user 1 thanks" //Should fail since only one value is there
let customermsg4 = "Dear user 435 4523 thanks" // With many spaces
console.log(regex.test(customermsg));
console.log(regex.test(customermsg1));
console.log(regex.test(customermsg2));
console.log(regex.test(customermsg3));
console.log(regex.test(customermsg4));
发布于 2021-02-24 19:10:13
https://regex101.com/r/qBFSxq/1
这可以是解决这个问题的一种方法:^Dear\s*user(\s*\d)?(?(1)(.{1,5})(?=(\s{1,}))(?<!(\s{2}))(.{1,5}) thanks|\s*thanks)$
编辑:添加正则表达式正向前视构造((.{1,5})(?=(\s{1,})))、https://www.regular-expressions.info/lookaround.html,以及在正则表达式内部使用条件((\s*\d)?(?(1)...)和负向后视((?<!(\s{2}))(.{1,5}))
发布于 2021-02-24 19:23:50
您可以对匹配前的部分使用s捕获组,并使用单个非空格字符开始匹配。
在右侧断言1-9个字符,后跟thanks。如果是这种情况,则至少匹配另一个非空格字符,然后匹配其余字符,直到到达thanks。
例如
let regex = /^(Dear\s*user\s*)\S(?=.{1,9} thanks$)\s*\S.*(?= thanks$)/i;
[
"Dear user 1 2 thanks",
"Dear user 12345 6789 thanks",
"Dear user 123 5 6789 thanks",
"Dear user 1 thanks",
"Dear user 1 thanks"
].forEach(s =>
console.log(s.replace(regex, (m, g1) => g1 + "{#val#}{#val#}"))
);
或者如果单个捕获组只能有数字和空格
let regex = /^(Dear\s*user\s*)\d(?=[ \d]*\d)[ \d]{1,9}(?= thanks$)/i;
[
"Dear user 1 2 thanks",
"Dear user 12345 6789 thanks",
"Dear user 123 5 6789 thanks",
"Dear user 1 thanks",
"Dear user 1 thanks",
"Dear user 12345 64789 thanks"
].forEach(s =>
console.log(s.replace(regex, (m, g1) => g1 + "{#val#}{#val#}"))
);
https://stackoverflow.com/questions/66348940
复制相似问题