From 27b24066a6d964e835a6691a3a1647a01bcee3ab Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Mon, 6 May 2013 20:19:21 -0400 Subject: [PATCH] make regexp var package-level, compiled once. fixes #11 --- query.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/query.go b/query.go index c1427a5..f085d99 100644 --- a/query.go +++ b/query.go @@ -7,6 +7,8 @@ import ( "strings" ) +var rxClassTrim = regexp.MustCompile("[\t\r\n]") + // Is() checks the current matched set of elements against a selector and // returns true if at least one of these elements matches. func (this *Selection) Is(selector string) bool { @@ -44,14 +46,12 @@ func (this *Selection) IsNodes(nodes ...*html.Node) bool { // HasClass() determines whether any of the matched elements are assigned the // given class. func (this *Selection) HasClass(class string) bool { - var rx = regexp.MustCompile("[\t\r\n]") - class = " " + class + " " for _, n := range this.Nodes { // Applies only to element nodes if n.Type == html.ElementNode { if elClass, ok := getAttributeValue("class", n); ok { - elClass = rx.ReplaceAllString(" "+elClass+" ", " ") + elClass = rxClassTrim.ReplaceAllString(" "+elClass+" ", " ") if strings.Index(elClass, class) > -1 { return true }