简体   繁体   English

如何检查字符串是否包含数组中的子字符串并将其拆分

[英]How to check if a string contains a substring from an array and split it

I have an array of string like: 我有一个像这样的字符串数组:

var matchCodFisc = ["C.F.", "CF", "Cod.Fisc.", "C.F"]

and I want to check if in my string (eg var myString= "Cod.Fisc.FGDTFR34L16G643W" ) exists that substring (in this case "Cod.Fisc"). 并且我想检查我的字符串中是否存在该子字符串(例如var myString= "Cod.Fisc.FGDTFR34L16G643W" )(本例中为“ Cod.Fisc”)。 If I found the substring I want to split my string in two parts like ["Cod.Fisc", "FGDTFR34L16G643W"] . 如果找到子字符串,我想将字符串分成两部分,例如["Cod.Fisc", "FGDTFR34L16G643W"] How can I do that using JavaScript? 如何使用JavaScript做到这一点?

You could find the string and take the rest. 您可以找到该字符串,然后剩下的字符串。 Maybe it is better to sort the pattern array by length descending, because it finds the longest string first. 也许最好按长度降序对模式数组进行排序,因为它会先找到最长的字符串。

 var matchCodFisc = ["CF", "CF", "Cod.Fisc.", "CF"], string = "Cod.Fisc.FGDTFR34L16G643W", found = matchCodFisc .sort((a, b) => b.length - a.length) .find(s => string.startsWith(s)), result = [found, string.slice(found.length)]; console.log(result); 

You can use find, includes and split 您可以使用查找,包含和拆分

  • First find the matching value from matchChodFisc using find and includes 首先使用findincludesmatchChodFiscfind匹配值
  • Create a regex pattern based on value of found 根据found值创建一个正则表达式模式
  • Split using regex we built and fillter empty values 使用正则表达式拆分,我们构建并填充空值

 var matchCodFisc = ["CF", "CF", "Cod.Fisc.", "CF"] var myString= "Cod.Fisc.FGDTFR34L16G643W" let found = matchCodFisc.find(val=> myString.includes(val)) let regex = new RegExp(`(${found})`, 'gi') let splited = myString.split(regex).filter(Boolean) console.log(splited) 

You could do with Array#findIndex method using Str.includes() 您可以使用Str.includes()使用Array#findIndex方法

 var matchCodFisc = ["CF", "CF", "Cod.Fisc.", "CF"] var myString = "Cod.Fisc.FGDTFR34L16G643W"; var resindex = matchCodFisc.findIndex(a => myString.includes(a)); console.log([ matchCodFisc[resindex], myString.split(matchCodFisc[resindex])[1] ]) 

Make your array into a regular expression: 使数组成为正则表达式:

 const matchCodFisc = ["CF", "CF", "Cod.Fisc.", "CF"]; const myString = "Cod.Fisc.FGDTFR34L16G643W"; const splitRegex = new RegExp('('+matchCodFisc.join('|')+')\\\\w+', 'g'); console.log(splitRegex); let result = splitRegex.exec(myString) console.log(result[1]) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查字符串是否包含JavaScript中的字符串数组中的子字符串 - how to check if a string contains a substring from an array of strings in JavaScript 如何检查字符串是否包含来自 NodeJS 中子字符串数组的文本,然后在数组中找到该 ZE83AED3DDF4667DEC0DAAAACB2BB3BE0BZ 的索引? - How to check if a string contains text from an array of substrings in NodeJS and then find the index of that substring in the array? 使用 Jest 检查字符串是否包含数组中的子字符串之一 - Check that a string contains one of the substring from an array with Jest 如何检查字符串是否包含子字符串并在javascript中为其着色? - how to check if a string contains substring and color it in javascript? 如何检查字符串是否包含特定的子字符串 - How to check if a string contains a specific substring 如何在JavaScript中检查字符串是否包含子字符串 - How to check string contains substring in javascript 如何检查一个字符串在JavaScript中是否包含一个substring? - How to check whether a string contains a substring in JavaScript? 如何检查数组是否包含子字符串? - How can one check if an array contains a substring? 检查字符串数组是否包含javascript中给定字符串的子字符串? - Check if an array of strings contains a substring of a given string in javascript? 检查字符串是否包含严格子字符串 - Check if a string contains a strict substring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM