Is it preferable to use methods for RegExp objects or String objects?

There are regular expression methods for both RegExp and String objects in Javascript.

RegExp objects have methods

  • Exec
  • test

String objects have 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 test and search 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 for using methods on RegExp objects or String objects?

+6
source share
2 answers

Private preferences

These are mostly personal preferences, although there is a slight difference in the possibilities (see the last paragraph for this difference). If you do not want to start benchmarking performance measurement and turn the question into one of the characteristics, these are mostly personal preferences.

I prefer to do:

 string.match(/word/) 

because I think the code reads how my brain thinks about the operation. I take a string object and look for a specific regular expression in it. It makes sense to me that finding something in a string is a string object method. This reads to me like this:

 object.action(tool) 

how object-oriented code should work, in my opinion.

If you have:

 /word/.match(string) 

then it seems to me:

 tool.action(object) 

which I find much less logical and less object oriented. Yes, /word/ also technically an object, but it is not an object that I am trying to work on, I think it is more like a tool or parameter that I use with my object.

Obviously both work. You can decide what you like.

One significant difference

There are a few things that I believe can only be done on the regular expression object. For example, if you want to match all occurrences of a regular expression, you need to create a regular expression object, pass the parameter "g" to it, and then call re.exec() several times to get each match until exec returns null that no longer indicates matches. I do not think you can do this with string.match() .

+5
source

When using the global flag g there are some differences between match and exec . You cannot scroll global occultations with match , and when using exec you can:

 var string="xabx xnnx"; var regexp=new RegExp('x([^x]*)x','g'); // match returns the list of matching parts: string.match(regexp) // ["xabx", "xnnx"] // with exec you could build a loop: var match=regexp.exec(string); while (match) { console.log(match); match=regexp.exec(string); } // This loops through matches and returns subpatterns too: // ["xabx", "ab"] // ["xnnx", "nn"] 

And index doesn't work at all when using match with a globalized RegExp object:

 var r=/b/g; r.exec("0123b56b").index // 4 r.exec("0123b56b").index // 7 "0123b56b".match(r).index // undefined "0123b56b".match(/b/).index // 4 
+2
source

Source: https://habr.com/ru/post/896926/


All Articles