如何根据特定条件分割字符串

时间:2020-05-12 16:25:51

标签: javascript reactjs

我有一个如下所示的动态字符串:

"intro":"1.[Model=FS7200 NVMe Control, IOGroups=1, SystemMemory=1536 GiB, AdapterType=16 Gb FC 4 Port Adapter Pair, P1_Type=DRP, P1_ExtentSize=1024 MiB, P1_ThinProvisioning=0-49%, P1_Compression=0-49%, P1_Deduplication=0-49%, P1A1_IOGrp=1]\nAn explanation could not be generated for this combination\nThe following error occurred: Problem in restriction !(IOGroups.equals(\"1\")) || (!P1A1_IOGrp.equals(\"1\") && !P1A1_IOGrp.equals(\"2\") && !P1A1_IOGrp.equals(\"3\")) && P2A1_IOGrp.equals(\"NA\") && P2_Type.equals(\"NA\") && P2_ExtentSize.equals(\"NA\") && P2_ThinProvisioning.equals(\"NA\") && P2_Compression.equals(\"NA\") && P2_Deduplication.equals(\"NA\") && P2A1_DriveValues.equals(\"NA\") && P2A1_DriveTechnology.equals(\"NA\") && P2A1_DriveType.equals(\"NA\") && P2A1_ArrayType.equals(\"NA\") && P2A1_TargetGroup.equals(\"NA\") && P2A1_StripSize.equals(\"NA\")\nResulting expression is too large to handle. Please consider simplifying your restriction by breaking it into several simpler ones\n\n"

在这里,我想根据以下内容进行拆分:

An explanation之后的文本应显示在下一行。

restriction之后和Resulting之前的文本应以蓝色显示,并显示在下一行。

Resulting之后的文本应在下一行显示为正常的黑色。

我能够根据第二个条件进行拆分,但不能完全拆分。

{item.intro.search('restriction') > 0 &&
  <h5>
   {i+1}> {item.intro.split('restriction')[0]}restriction
   <span style={{color:"blue"}}>
   {item.intro.split('restriction')[1]}
   </span>
  </h5>}

我该如何解决?

1 个答案:

答案 0 :(得分:0)

就我的用例而言,您可以将 str1 替换为起始词,并将 str2 替换为结束词。

newtext 中,您将获得str1和str2之间的字符串

var re = /(.*word1\s+)(.*)(\s+word2.*)/;
var newtext = item.intro.replace(re, "$2");

您可以尝试jsfiddle中提供的解决方案 并尝试根据您的用例进行更改。

该解决方案是从Regular expression to get a string between two strings in Javascript 引用的

capturing group with a lazy dot matching pattern中也提供了详细说明。