Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.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_Substring - Fatal编程技术网

Javascript 基于输入从模式中提取部分字符串

Javascript 基于输入从模式中提取部分字符串,javascript,regex,substring,Javascript,Regex,Substring,例1 string: "999 someString" pattern: "{id} {value}" key: "id" expected result: "999" 例2 string: "D-12345 ABcdE" pattern: "D-{id} {value}" key: "value" expected result: "ABcdE" 例3 string: "abcde 000-strING" pattern: "abcde {id}-{value}" key: "value"

例1

string: "999 someString"
pattern: "{id} {value}"
key: "id"
expected result: "999"
例2

string: "D-12345 ABcdE"
pattern: "D-{id} {value}"
key: "value"
expected result: "ABcdE"
例3

string: "abcde 000-strING"
pattern: "abcde {id}-{value}"
key: "value"
expected result: "strING"
例4

string: "aaa-123-bbb"
pattern: "aaa-{id}-{value}"
key: "id"
expected result: "123"
我想写一个函数,可以接收字符串、模式和键,这个函数可以返回预期的结果。 差不多

const stringExtractor = (string, pattern, key) => {
...
return result
}

我知道这可能需要正则表达式来完成,但我不知道如何定义正则表达式。

最差的代码效果最好

警告:如果模式类似于{id}{value},则代码失败

真的想从其他社区成员那里看到好的答案,比如使用Regex 设e1={string:999 someString,模式:{id}{value},键:id}; 设e2={string:D-12345 ABcdE,pattern:D-{id}{value},key:value}; 设e3={ 字符串:abcde 000字符串, 模式:abcde{id}-{value}, 关键:价值,, }; 设e4={string:aaa-123-bbb,pattern:aaa-{id}-{value},key:id}; 常量stringExtractor={string,pattern,key}=>{ 设tempKey=`{${key}}`; 设tempString=string; 让keyStartIndex=pattern.indexOftempKey; 设keyLastIndex=keyStartIndex+tempKey.length; let before=pattern.substring0,keyStartIndex; let after=pattern.substringkeyLastIndex; 让isBeforeMatch=string.includesbefore; 让isAfterMatch=string.includeAfter; tempString=tempString.replacebefore; tempString=tempString.replaceafter; 如果是在比赛之前{ tempString=tempString.splitafter.substring0,1[0]; }否则,如果我不匹配{ 设_l=before.length-1; tempString=tempString.splitbefore.substring_l[1]; } 返回临时字符串; }; console.logExample 1=>,stringExtractore1; console.logExample 2=>,stringExtractore2; console.logExample 3=>,stringExtractore3;
console.logExample 4=>,stringExtractore4 代码中的逻辑注释。正在查找{id}和{value}之间的分隔符。然后从模式中查找所有其他元素,并在字符串中替换相同的元素。然后使用分隔符拆分字符串并基于键返回

const stringExtractor = (string, pattern, key) => {
  let seperator = pattern.substr(pattern.indexOf('{'), pattern.lastIndexOf('}')).replace(/{id}|{value}/g, ""); // finding the separator between {id} and {value}
  let otherElements = pattern.replace("{id}" + seperator + "{value}", ""); // finding other elements other than our {id} {value}
  if (!!otherElements) {
    string = string.replace(otherElements, ""); // removing other elements
  }
  let resultArray = string.split(seperator);
  return key == 'id' ? resultArray[0] : resultArray[1]; // this is hardcoded, can make dynamic
}
您可以使用/[^}]*{[^}]+}[^{]*/从模式中获取每个块。块是{}之前的分隔符、{}内部的键和{}之后的分隔符,并获取这些部分中的每个部分以分离匹配的组。使用exec和循环遍历每个块

函数字符串提取器字符串、模式、键{ 设block=/[^}]*{[^}]+}[^{]*/g, 模式匹配; 而patternMatch=block.execpattern{ 常数[之前,k,之后]=模式匹配, partial=新的RegExp`${before}.*.${before |$'}`; 如果k==key 返回字符串。matchpartial[1] 其他的 string=string.replacenew RegExppartial, } } console.logstringExtractor999 someString,{id}{value},value console.logstringExtractorD-12345 ABcdE,D-{id}{value},id console.logstringExtractoraaa-123-bbb,aaa-{id}-{value},id console.logstringExtractor135236-374,1{v1}2{v2}-{v3}4,v2
console.logstringExtractorabc-234d-100efg,ab{v1}d-10{v2},v2我看不到模式的一致性遗憾的是,您必须在模式中定义模式是的,字符串来自模式,但模式并不总是相同的。Id和值可能总是出现在模式中,但可能会附加到模式中,使模式看起来不同。嗨,如果分隔符也是值的一部分,是否可能?如示例`//输出应为'some String'stringExtractor999 some String,{id}{value},值'@willie01228,当分隔符位于不相关的键内时,我已更新该值