Munir Abrar
Munir Abrar

Reputation: 81

Javascript Regex Search

I generated the following code through a website. What I am looking for is that the script scans through a text variable against a set of keywords, and if it finds any of the keywords, it passes it to a variable. And if two keywords are found, both are joined by a hyphen and passed to a variable. I also need to set the "var str" dynamically. For instance, "var str == VAR10." VAR10 will have a dynamic text to be searched for keywords.

var re = /Geo|Pete|Rob|Nick|Bel|Sam|/g;
var str = 'Sam maybe late today. Nick on call. ';
var m;

if ((m = re.exec(str)) !== null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }
 }

In the above code, Sam and Nick are two keywords that I want hyphenated and passed to VAR10.

Upvotes: 0

Views: 98

Answers (4)

Drakes
Drakes

Reputation: 23670

If two keywords are found, both are joined by a hyphen and passed to a variable

Try this update to your original code for clarity:

var re = /Geo|Pete|Rob|Nick|Bel|Sam/g;
var str = 'Sam maybe late today. Nick on call. ';
var m;
var VAR10 = ""; // holds the names found

if ((m = re.exec(str)) !== null) {
    var name1 = m;    

    if ((m = re.exec(str)) !== null) {
        var name2 = m; 
        // Two names were found, so hyphenate them
        // Assign name1 + "-" + name2 to the var that you want
        VAR10 = name1 + "-" + name2;
    } else {
        // In the case only one name was found:
        // Assign name1 to the var that you want
        VAR10 = name1;
    }
 }

Note, change

var re = /Geo|Pete|Rob|Nick|Bel|Sam|/g;

to

var re = /Geo|Pete|Rob|Nick|Bel|Sam/g;

Here is an updated demo: http://jsfiddle.net/7zg2hnt6/1/

Upvotes: 1

Jim Schubert
Jim Schubert

Reputation: 20367

Your code is almost doing what you want.

First you need to capture your matches, then join them. http://jsfiddle.net/c6tjk21d/1/

var re = /(Geo|Pete|Rob|Nick|Bel|Sam)/g;
var str = 'Sam maybe late today. Nick on call. ';
var VAR10 = str.match(re).join('-')
console.log(VAR10);

I don't think you want to use exec because it maintains state and I've found it to be unintuitive. For example, in order to get more than one match with the code you've written, you'll need to loop through resulting on exec. Check out MDN for examples if you're interested. I almost always prefer match().

Upvotes: 0

user663031
user663031

Reputation:

To return the first two names found in hyphenated fashion:

str.match(re) . slice(0, 2) . join('-')

You have an extra | at the end of your regexp, which is likely to result in matches on an empty string. Remove it.

I also need to set the "var str" dynamically. For instance, "var str == VAR10." VAR10 will have a dynamic text to be searched for keywords.

var str == VAR10 is invalid syntax. I'll assume you mean var str = VAR10;. That's just a plain old variable assignment. All assignments in JS are "dynamic" by definition and happen at run-time. This would seem to have nothing to do with your specific problem.

Upvotes: 0

fdglefevre
fdglefevre

Reputation: 692

You can "capture" names with parenthesis:

/(Geo|Pete|Rob|Nick|Bel|Sam)/g

A sample: https://regex101.com/r/eK5hY2/1

Upvotes: 0

Related Questions