Home > Software engineering >  Searching text with regex which is starting with specific pattern
Searching text with regex which is starting with specific pattern

Time:09-26

I am searching the word inside the text using specific keyword.

const matchHashtags = /(#\w ) ?/g;
const text ="this is #sample tag and other #another tag";
while ((hashtag = matchHashtags.exec(text))) {
      alert(hashtag);
}

As in above, we are searching using the word starting with # and it is working fine. But with the below code, I am searching for the text starting with "iphone~".

const matchHashtags2 = /(^|\s)\iphone~(\w )/g;
const text2 ="I have iphone~seven~new and iphone~seven~ old phones";
while ((hashtag2 = matchHashtags2.exec(text2))) {
      alert(hashtag2);
}

I am expecting this to search for iphone~seven i.e to include next word after iphone~. But this query is returning output as 3 values: "iphone~seven, ,seven".

https://jsfiddle.net/gecj54a3/1/

Please assist in resolving the issues. Thank you!

CodePudding user response:

What´s wrong with const matchHashtags2 = /(iphone~\w ) ?/g;?

CodePudding user response:

I would do something like this:

var text2 = 'I have iphone~seven~new and iphone~seven~old phones';
var myArray2 = text2.split(' ');
for (var i = 0; i < myArray2.length; i  ){
    if(myArray2[i].includes('iphone')){
        alert(myArray2[i]);
    }
}

CodePudding user response:

Try this:

const matchHashtags2 = /(^|\s)\iphone~(\w )/g;
const text2 ="I have iphone~seven~new and iphone~seven~ old phones";
while ((hashtag2 = matchHashtags2.exec(text2))) {
      alert(hashtag2[2]); // hashtag2 is an array
}

CodePudding user response:

The reason for that result in the alert is because you are using a capture group.

Also note that in your code you don't need the extra set of parenthesis for the while loop, and in the pattern you don't have to escape \i to match an i char.

If you use (^|\s) and there is a leading space, it will be part of the match. What you can do is make that part a non capture group, and use a capture group for the part that you are interested in.

Then use console.log to see the group 1 value.

const matchHashtags2 = /(?:^|\s)(iphone~\w )/g;
const text2 = "I have iphone~seven~new and iphone~seven~ old phones";
let hashtag2;
while (hashtag2 = matchHashtags2.exec(text2)) {
  console.log(hashtag2[1]);
}

Or you can write the code using match without a capture group and still with the global flag /g preceded with a word boundary \b if that is also acceptable for the matches.

const matchHashtags2 = /\biphone~\w /g;
const text2 = "I have iphone~seven~new and iphone~seven~ old phones";
console.log(text2.match(matchHashtags2));

  • Related