Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm working on an angular app, which I kind of inherited. I saw that there was a bug with unescaped regex so I wanted to add a function for escaping regex like so:

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[]\]/g, '\$&'); // $& means the whole matched string
}

No big deal, right? But where to put this? The regex is used inside a controller. But does it make sense to have this escapeRegExp function inside the scope like so:

                    $scope.escapeRegExp = function(string) {
                      return string.replace(/[.*+?^${}()|[]\]/g, '\$&'); // $& means the whole matched string
                    }

I'm a little confused where best to put these little functions. What's the best practice?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
428 views
Welcome To Ask or Share your Answers For Others

1 Answer

If you expect to reuse this piece of code - put it to the separate module (utils/common/shared) as a service

If you expect to use this piece of code only for this controller - extract it to the separate file as a function


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...