-1

I have a pretty long string(called 'my_string') without new lines included. I have been trying to use regexp in JavaScript to find specific words in 'my_string'. Below is the code description

my_string=`Type of reaction engine discharging a fast-moving jet that generates thrust by jet propulsion. While this broad definition can include rocket, water jet, and hybrid propulsion, the term jet engine typically refers to an internal combustion airbreathing jet engine such as a turbojet, turbofan, ramjet, or pulse jet.[1] In general, jet CP_COLOR_B[#240025: engines]CP_COLOR_E are internal combustion engines. jet engines typically feature a rotating air CP_COLOR_B[#254117: compressor]CP_COLOR_E powered by a turbine, with the leftover power providing thrust through the propelling nozzle—this CP_COLOR_B[#2424df: combined]CP_COLOR_E is known as the Brayton thermodynamic cycle.`; 

var free_10_array = my_string.split("\n");
var free_10 = '';
for(var _10 in free_10_array){
   free_10+=free_10_array[_10];
}

const my_patt = new RegExp("(CP_COLOR_B\\[.+\\]CP_COLOR_E)", "gi");
var result_array = my_string.match(my_patt);
console.log(result_array);

I am expecting the array result_array to be a length of three(3) since there are only three(3) patterns(CP_COLOR_B[#240025: engines]CP_COLOR_E, CP_COLOR_B[#254117: compressor]CP_COLOR_E and CP_COLOR_B[#2424df: combined]CP_COLOR_E) I am aiming to find. Instead, I got an array of one element with other strings attached to only the first matched pattern. I will love to have the result in this form, such as:

output=['CP_COLOR_B[#240025: engines]CP_COLOR_E','CP_COLOR_B[#254117: compressor]CP_COLOR_E','CP_COLOR_B[#2424df: combined]CP_COLOR_E']
Glenn Ferrie
  • 9,617
  • 3
  • 37
  • 67
  • You can create a regular expression that will help you find matches in the output -- here is a quick intro to regular expressions: https://www.oreilly.com/content/an-introduction-to-regular-expressions/ -- more info RegExp in JS from MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp – Glenn Ferrie May 10 '21 at 13:23
  • Please format your first code extract with different lines so that it's readable. – Pierre May 10 '21 at 13:24
  • 1
    Your regex (specifically, the `+` quantifier) is "greedy", but you want it to be "lazy". See [What do 'lazy' and 'greedy' mean in the context of regular expressions?](https://stackoverflow.com/questions/2301285/what-do-lazy-and-greedy-mean-in-the-context-of-regular-expressions) – IMSoP May 10 '21 at 13:29
  • @Pierre I tried to do the formatting but the system wasn't responding too good – Agbesi Innocent May 10 '21 at 13:31

1 Answers1

1

Other than a few minor mistakes in your regex, you need to use .+? instead of .+, because the second one is "greedy" which means, it will match as much as it can get.

my_string=`Type of reaction engine discharging a fast-moving jet that generates thrust by jet propulsion. While this broad definition can include rocket, water jet, and hybrid propulsion, the term jet engine typically refers to an internal combustion airbreathing jet engine such as a turbojet, turbofan, ramjet, or pulse jet.[1] In general, jet CP_COLOR_B[#240025: engines]CP_COLOR_E are internal combustion engines. jet engines typically feature a rotating air CP_COLOR_B[#254117: compressor]CP_COLOR_E powered by a turbine, with the leftover power providing thrust through the propelling nozzle—this CP_COLOR_B[#2424df: combined]CP_COLOR_E is known as the Brayton thermodynamic cycle.`;

// Note the ".+?"
const my_patt = /CP_COLOR_B\[.+?\]CP_COLOR_E/g;

const result_array = my_string.match(my_patt);

console.log(result_array);
console.log(result_array.length);

Here is a better explanation on greedy vs lazy: What do 'lazy' and 'greedy' mean in the context of regular expressions?

MauriceNino
  • 4,927
  • 1
  • 14
  • 44