[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

34.6.1 Replacing the Text that Matched

This function replaces all or part of the text matched by the last search. It works by means of the match data.

Function: replace-match replacement &optional fixedcase literal string subexp

This function replaces the text in the buffer (or in string) that was matched by the last search. It replaces that text with replacement.

If you did the last search in a buffer, you should specify nil for string and make sure that the current buffer when you call replace-match is the one in which you did the searching or matching. Then replace-match does the replacement by editing the buffer; it leaves point at the end of the replacement text, and returns t.

If you did the search in a string, pass the same string as string. Then replace-match does the replacement by constructing and returning a new string.

If fixedcase is non-nil, then replace-match uses the replacement text without case conversion; otherwise, it converts the replacement text depending upon the capitalization of the text to be replaced. If the original text is all upper case, this converts the replacement text to upper case. If all words of the original text are capitalized, this capitalizes all the words of the replacement text. If all the words are one-letter and they are all upper case, they are treated as capitalized words rather than all-upper-case words.

If literal is non-nil, then replacement is inserted exactly as it is, the only alterations being case changes as needed. If it is nil (the default), then the character ‘\’ is treated specially. If a ‘\’ appears in replacement, then it must be part of one of the following sequences:

\&

\&’ stands for the entire text being replaced.

\n

\n’, where n is a digit, stands for the text that matched the nth subexpression in the original regexp. Subexpressions are those expressions grouped inside ‘\(…\)’. If the nth subexpression never matched, an empty string is substituted.

\\

\\’ stands for a single ‘\’ in the replacement text.

These substitutions occur after case conversion, if any, so the strings they substitute are never case-converted.

If subexp is non-nil, that says to replace just subexpression number subexp of the regexp that was matched, not the entire match. For example, after matching ‘foo \(ba*r\)’, calling replace-match with 1 as subexp means to replace just the text that matched ‘\(ba*r\)’.


[ < ] [ > ]   [ << ] [Plus haut] [ >> ]         [Top] [Table des matières] [Index] [ ? ]

Ce document a été généré par Eric Reinbold le 13 Octobre 2007 en utilisant texi2html 1.78.