Aditi
Aditi

Reputation: 21

How to split the string based on a specific condition

I am having a dynamic string like below:

"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"

Here, I want to split it on following basis:

The text after An explanation should show in next line.

The text after restriction, and before Resulting should show in blue color, and in next line.

The text after Resulting should show in normal black color, and in next line.

I am able to do splitting based on second condition, but not completely.

{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>}

How can I resolve this?

Upvotes: 0

Views: 70

Answers (1)

Shivam Pathak
Shivam Pathak

Reputation: 73

As far as I get your use case, you can replace str1 with your starting word and str2 with your ending word.

In newtext you will get the string between str1 and str2

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

you can try the solution available in jsfiddle and try to make changes as per your use case.

The solution is referenced from Regular expression to get a string between two strings in Javascript

A detailed explanation is also available in this capturing group with a lazy dot matching pattern.

Upvotes: 1

Related Questions