1

Before you mark this as duplicate, let me assure you that I have gone through, hopefully, all of the questions asked on the subject on stackoverflow, including the following:

How can I invert a regular expression in JavaScript?

negation in regular expression

I am trying to parse a string with syntax as follows:

 contents = "Some Random Text1 <@@Matched text 1@@> Some Random Text2 <@@Matched text 2@@> Some Random Text3";

I used the regular express:

 reg_exp = /\<\!\!.*?\!\!\>/g   

and then used the following code to extract the Matched Text and make a long string out of it with a blank delimiter in between each match.

 while ((matched_array = reg_exp.exec(contents)) != null) {
     matched_text +=  (matched_array[0] + "");
 };

This returns matched_text as:

  <@@Matched text 1@@> <@@Matched text 2@@> 

All this works fine. Now I want to get a substring of all text outside the matched text.

I cannot set a regular expression to do the same with the text outside the matched text, to generate the following result string for the above example:

  Some Random Text1 Some Random Text2 Some Random Text3

I tried all possible solutions provided in the above cited posts, including (?! or ^ after grouping (), etc.

Community
  • 1
  • 1
Sunny
  • 9,245
  • 10
  • 49
  • 79

1 Answers1

2

Use string.replace function to replace those matched chars with an empty string.

var string =  "Some Random Text1 <@@Matched text 1@@> Some Random Text2 <@@Matched text 2@@> Some Random Text3";
alert(string.replace(/\s*<@@.*?@@>/g, ''))
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Certainly this should do the job but I just want to wait and see if there is a way to negate the regular expression used which can also do the job. If no other answer along this line comes along, I will accept your answer. Actually, your answer is easy to understand and very logical too. – Sunny Sep 23 '15 at 17:40
  • 1
    generally, when needed, you should use many RegExps instead one giant monstrosity for both performance and readability. – dandavis Sep 23 '15 at 17:53
  • @dandavis, Can you suggest how one can handle my problem with multipe regex? – Sunny Sep 23 '15 at 18:07