0

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.

1
  • 1
    What's the expected output in this case? Commented Mar 30, 2015 at 3:28

4 Answers 4

1

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/

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Drakes, it works great but if there is only keyword, than it doesn't return anything. Secondly, one keyword or two keywords, at the end, how could I assign the result to a new variable to be passed on to something else?
Hi Munir, I updated my answer. It will handle a case of two names, or just one name as requested. Question: is this new variable created outside this code block?
Thanks again Drakes for the quick fix. It's perfect. One last thing if you don't mind please, how about if no keyword is found? Can we assign a static text to VAR10 in that case?
Yes. At the top of the code block there is var VAR10 = ""; for this no-name case. Change it to the text you would like.
0

You can "capture" names with parenthesis:

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

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

2 Comments

It doesn't change anything.
It's a way to find all names in his string. They are more than one question here. :)
0

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.

1 Comment

You are right torazaburo, the syntax was wrong. Thanks for all the info :)
0

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().

3 Comments

Thanks Jim for jumping in. It's very neat. I just requested Drakes to consider what if no keyword is found? Can we assign a static text to VAR10 in that case?
@MunirAbrar No problem. The code I posted wouldn't limit to two found names. If you want to provide a default, you'd need to check the matches and if none are found, set your default string. Here's an update to the fiddle which shows how this would be done: jsfiddle.net/c6tjk21d/2
Thanks Jim, I saw it that it grabs as many keywords as found, not limited to a certain number. And now it has the default option also in case nothing found. Works real good. Thanks a lot man :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.