Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8320511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:40:27+00:00 2026-06-08T22:40:27+00:00

_(test1: + (slayers get more.match(RegExp(^ + slayers get magic), g))); I am expecting the

  • 0
_("test1: "+ ("slayers get more".match(RegExp("^" + "slayers get magic"), "g")));

I am expecting the result “slayers get m” but instead I get null?

Typing a regexp literal instead does work.

Another question: How do I get it to match whole words only, so that the result would be “slayers get” ?

thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T22:40:29+00:00Added an answer on June 8, 2026 at 10:40 pm

    The "g" should be the second argument to RegExp(), not the second argument to .match(). (Though with your regex starting with a ^ to match from the beginning of the string you don’t need "g" at all.)

    In addition it would be “more correct” to use new as follows:

    _("test1: "+ ("slayers get more".match(RegExp("^" + "slayers get magic", "g"))));
    

    However I believe in the case of RegExp() it works with or without new.

    ‘I am expecting the result “slayers get m” but instead I get null?’

    null is the correct result for that string and that regex, because it doesn’t match. That is, a regex is either going to match or not (in this case not), it doesn’t return the portion of the string that made a partial match.

    It seems like what you really want to do is return that part of the first string that matches the beginning of the second string, up to but not including the first different character. If so, try this:

    function getCommon(s1,s2) {
       var i = 0, len = s1.length;
       while (i < len && s1.charAt(i) === s2.charAt(i))
          i++;
       return s1.substr(0,i);
    }
    
    _("test1: "+ getCommon("slayers get more", "slayers get magic"));
    

    ‘Another question: How do I get it to match whole words only, so that the result would be “slayers get”?’

    Building on the same idea as my previous function, just compare one word at a time:

    function getCommon(s1,s2) {
       var a1 = s1.split(" "),
           a2 = s2.split(" "),
           len = a1.length,
           i = 0;
       while (i < len && a1[i] === a2[i])
          i++;
       return a1.slice(0,i).join(" ");
    }
    

    This splits both strings up into individual words, compares one word at a time until there is a mismatch, and returns the words that matched up until that point (or an empty string if none matched).

    DEMO: http://jsfiddle.net/j8zsU/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

why does the following js expression: test1 foo bar test2.replace(/foo.bar/, $') result in the
I tried this: test1.pl >output.log 2>&1 but this is the result: Can't dup STDOUT:
I have a bunch of unit tests (Test1, Test2, etc.), but rather than having
alert('test1'); var re = new RegExp((http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?), si); alert('test2'); Why is this happening? How to
def test1(a: Any) = a match { case x: AnyRef => AnyRef case _
class Test1: def __init__(self): self.x = 1 class Test2(Test1): # how can I get
Consider the following code: public abstract class Test1 { public object Data { get;
I want to get NSString * test1 = @one; NSString * test2 = @two;
I have such string test1/test2/test3/test4/test5 How can I get those tests in separate variables
1) Why code under /* test1 */ chunk do not print out anything, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.