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 6564107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:55:32+00:00 2026-05-25T13:55:32+00:00

There are regex matching methods on both RegExp objects and String objects in Javascript.

  • 0

There are regex matching methods on both RegExp objects and String objects in Javascript.

RegExp objects have the methods

  • exec
  • test

String objects have the methods

  • match
  • search

The exec and match methods are very similar:

/word/.exec("words");
//Result: ["word"]
"words".match(/word/);
//Result: ["word"]

/word/.exec("No match");
//Result: null
"No match".match(/word/);
//Result: null

/word/g.exec("word word");
//Result: ["word"]
"word word".match(/word/g);
//Result: ["word", "word"]

and the test and search are also very similar:

/word/.test("words");
//Result: true
"words".search(/word/);
//Result: 0
//Which can converted to a boolean:
"word".search(/word/) > -1;
//Result: true

/word/.test("No match");
//Result: false
"No match".search(/word/) > -1;
//Result: false

Is there a preference to use the methods on RegExp objects or on String objects?

  • 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-05-25T13:55:33+00:00Added an answer on May 25, 2026 at 1:55 pm

    Mostly Personal Preference

    This is mostly personal preference, though there is a slight difference in capabilities (see last paragraph for that difference). Unless you want to start benchmarking performance measurements and turn the question into one of performance, it’s mostly personal preference.

    I prefer to do:

    string.match(/word/)
    

    because I think that makes the code read like my brain thinks of the operation. I’m taking the string object and looking for a particular regular expression in it. It makes sense to me that looking for something in a string is a method on the string object. This reads to me like:

    object.action(tool)
    

    which is how object oriented code should work in my opinion.

    If you have:

    /word/.match(string)
    

    then to me it feels like:

    tool.action(object)
    

    which I find a lot less logical and less object-oriented. Yes, /word/ is technically an object too, but it’s not the object I’m trying to operate on, I think of it more like a tool or a parameter I’m using with my object.

    Obviously both work. You can decide for yourself which you like.

    One Major Difference

    There are a few things that I believe you can only do on a regular expression object. For example, if you want to match all occurrences of a regular expression, you have to create the regular expression object, pass it the “g” option and then call re.exec() multiple times to get each match until exec returns null indicating no more matches. I don’t think you can do that with string.match().

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

Sidebar

Related Questions

I know there are some online regex evaluators.. very useful, matching in real time.
There's many regex's out there to match a URL. However, I'm trying to match
I have noticed in the PHP regex library there is a choice between ereg
There is a method Regex.Replace(string source, string pattern, string replacement) The last parameter supports
Is there a way to pass a variable into a regex in jQuery/Javascript? I
Is there a way to run a regexp-string replace on the current line in
I've seen MySQL SELECT examples using the REGEXP operator for matching. Is there a
Is there a DOTALL matching flag for boost::regex? The documentation shows: static const match_flag_type
I'm wondering if there is some kind of way to do fuzzy string matching
I want to implement a regex matching function in javascript that matches one or

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.