Function: word-search-regexp

word-search-regexp is a byte-compiled function defined in isearch.el.gz.

Signature

(word-search-regexp STRING &optional LAX)

Documentation

Return a regexp which matches words, ignoring punctuation.

Given STRING, a string of words separated by word delimiters, compute a regexp that matches those exact words separated by arbitrary punctuation. If the string begins or ends in whitespace, the beginning or the end of the string matches arbitrary whitespace. Otherwise if LAX is non-nil, the beginning or the end of the string need not match a word boundary.

Used in word-search-forward, word-search-backward, word-search-forward-lax, word-search-backward-lax.

Probably introduced at or before Emacs version 24.4.

Source Code

;; Defined in /usr/src/emacs/lisp/isearch.el.gz
;; Word search

(defun word-search-regexp (string &optional lax)
  "Return a regexp which matches words, ignoring punctuation.
Given STRING, a string of words separated by word delimiters,
compute a regexp that matches those exact words separated by
arbitrary punctuation.  If the string begins or ends in whitespace,
the beginning or the end of the string matches arbitrary whitespace.
Otherwise if LAX is non-nil, the beginning or the end of the string
need not match a word boundary.

Used in `word-search-forward', `word-search-backward',
`word-search-forward-lax', `word-search-backward-lax'."
  (cond
   ((equal string "") "")
   ((string-match-p "\\`\\W+\\'" string) "\\W+")
   (t (concat
       (if (string-match-p "\\`\\W" string) "\\W+"
	 "\\<")
       (mapconcat 'regexp-quote (split-string string "\\W+" t) "\\W+")
       (if (string-match-p "\\W\\'" string) "\\W+"
	 (unless lax "\\>"))))))