Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我如何在这个正则表达式中编程一种转义字符?_Javascript_Regex_Escaping_Regex Lookarounds - Fatal编程技术网

Javascript 我如何在这个正则表达式中编程一种转义字符?

Javascript 我如何在这个正则表达式中编程一种转义字符?,javascript,regex,escaping,regex-lookarounds,Javascript,Regex,Escaping,Regex Lookarounds,我想实现一个函数,从输入字符串(如“str1”)以数组的形式输出相应的字符串|str2@str3“: 但是,对于输入的字符串,只需要存在str1str2和str3(及其分隔符)都是可选的。为此,我已经编写了一个执行某种拆分的正则表达式。我无法进行(正常)拆分,因为分隔符是不同的字符,而且str1、str2和str3的顺序也很重要。这与我的正则表达式模式有点相似。现在,我正在努力扩展这个模式,以便您可以使用\|或\@来避开这两个分隔符 我怎样才能最好地解决这个问题 var strings = [

我想实现一个函数,从输入字符串(如“str1”)以数组的形式输出相应的字符串|str2@str3“:

但是,对于输入的
字符串
,只需要存在
str1
str2
str3
(及其分隔符)都是可选的。为此,我已经编写了一个执行某种拆分的正则表达式。我无法进行(正常)拆分,因为分隔符是不同的字符,而且str1、str2和str3的顺序也很重要。这与我的正则表达式模式有点相似。现在,我正在努力扩展这个模式,以便您可以使用\|或\@来避开这两个分隔符

我怎样才能最好地解决这个问题

var strings = [
  'meaning',
  'meaning|description',
  'meaning@id',
  'meaning|description@id',
  '|description',
  '|description@id',
  '@id',
  'meaning@id|description',
  'sub1\\|sub2',
  'mea\\|ning|descri\\@ption',
  'mea\\@ning@id',
  'meaning|description@identific\\|\\@ation'
];

var pattern = /^(\w+)(?:\|(\w*))?(?:\@(\w*))?$/ // works without escaping
console.log(pattern.exec(strings[3]));

根据问题定义,字符串0-3和8-11应该有效,其余的则无效
myFunc(strings[3])
并应返回
['means'、'description'、'id']
myFunc(strings[8])
应返回
[sub1\| sub2,null,null]
我猜您希望拆分所有字符串,我们可能会在char类中添加这些分隔符,类似于:

([|@\\]+)?([\w]+)
如果我们不这样做,我们可能希望这样做的验证,否则我们的验证将变得非常复杂,因为组合将增加

const regex=/([|@\\]+)?([\w]+)/gm;
const str=`意思
意义|描述
meaning@id
意义|description@id
|描述
|description@id
@身份证
meaning@id|描述
sub1\\| sub2
测量描述
意味着\\@ning@id
意义|description@identific\\|\\@",;
让m;
while((m=regex.exec(str))!==null){
//这是避免具有零宽度匹配的无限循环所必需的
if(m.index==regex.lastIndex){
regex.lastIndex++;
}
//可以通过'm`-变量访问结果。
m、 forEach((匹配,组索引)=>{
log(`Found match,group${groupIndex}:${match}`);
});

}
您需要允许模式中的
\[\\\[\@]
旁边的
\w
将您的
\w
替换为
(?:\\[\\\\[\\\\]\w)
模式:

var字符串=[
“意思”,
“意义|描述”,
'meaning@id',
”“什么意思|description@id',
|说明",,
'|description@id',
“@id”,
'meaning@id|说明',
“sub1\\| sub2”,
“mea\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\,
“我\\@ning@id',
”“什么意思|description@identific\\|\\@阿齐奥
];
变量模式=/^((?:\\[\\\\]\\\\w)+(?:\\\\.(?:\\[\\\\\\]\\\\w)*)?(?:@((?:\[\\\\]\\\w)*))?$/;
for(字符串的变量s){
if(型式试验){
console.log,“=>匹配项”);
}否则{
console.log,“=>失败”);
}

}
看起来你要找的可能是这个

(((?:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
说明: 匹配所有集,包括
“\@”
“\\\\”
,或除
“@”
“\\\”
之外的任何字符


您是否意识到字符串没有
\
s?您需要在字符串文本中加倍
\
,才能输入一个反斜杠。否。我已经试着用负面表情来扩展我的模式。但是我不能让它工作。看到了。谢谢:)你能帮我处理@wiktor吗?OP提到订单很重要,所以,不,这不是预期的。
([|@\\]+)?([\w]+)