Usage of JavaScript replace string replacement function

巴扎黑
Release: 2016-11-25 11:09:04
Original
1240 people have browsed it

replace
语法 stringObj.replace(rgExp, replaceText)
stringObj 必选项。要执行该替换的 String 对象或文字。该对象不会被 replace 方法修改。
rgExp 必选项。描述要查找的内容的一个正则表达式对象。
replaceText 必选项。是一个String 对象或文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。
例如:

Js代码

这样只能替换第一个“a”字母

这样可以替换所有“a”字母。其中g为全局标志



JavaScript--正则表达式

  正则表达式(regular expression)对象包含一个正则表达式模式(pattern)。它具有用正则表达式模式去匹配或代替一个串(string)中特定字符(或字符集合)的属性(properties)和方法(methods)。

正则表达式构造函数: new RegExp("pattern"[,"flags"]);
参数说明:
pattern -- 一个正则表达式文本
flags -- 如果存在,将是以下值:
g: 全局匹配
i: 忽略大小写
gi: 以上组合

在构造函数中,一些特殊字符需要进行转意(在特殊字符前加"\")。正则表达式中的特殊字符:
字符 含意
\ 转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为

匹配一个单词的边界。
-或-
对正则表达式功能字符的还原,如"*"匹配它前面元字符0次或多次,/a*/将匹配a,aa,aaa,加了"\"后,/a\*/

将只匹配"a*"。
^ 匹配一个输入或一行的开头,/^a/匹配"an A",而不匹配"An a"
$ 匹配一个输入或一行的结尾,/a$/匹配"An a",而不匹配"an A"
* 匹配前面元字符0次或多次,/ba*/将匹配b,ba,baa,baaa
+ 匹配前面元字符1次或多次,/ba*/将匹配ba,baa,baaa
? 匹配前面元字符0次或1次,/ba*/将匹配b,ba
(x) 匹配x保存x在名为$1...$9的变量中
x|y 匹配x或y
{n} 精确匹配n次
{n,} 匹配n次以上
{n,m} 匹配n-m次
[xyz] 字符集(character set),匹配这个集合中的任一一个字符(或元字符)
[^xyz] 不匹配这个集合中的任何一个字符
[\b] 匹配一个退格符
\b 匹配一个单词的边界
\B 匹配一个单词的非边界
\cX 这儿,X是一个控制符,/\cM/匹配Ctrl-M
\d 匹配一个字数字符,/\d/ = /[0-9]/
\D 匹配一个非字数字符,/\D/ = /[^0-9]/
\n 匹配一个换行符
\r 匹配一个回车符
\s 匹配一个空白字符,包括\n,\r,\f,\t,\v等
\S 匹配一个非空白字符,等于/[^\n\f\r\t\v]/
\t 匹配一个制表符
\v 匹配一个重直制表符
\w 匹配一个可以组成单词的字符(alphanumeric,这是我的意译,含数字),包括下划线,如[\w]匹配"$5.98"

中的5,等于[a-zA-Z0-9]
\W 匹配一个不可以组成单词的字符,如[\W]匹配"$5.98"中的$,等于[^a-zA-Z0-9]。

说了这么多了,我们来看一些正则表达式的实际应用的例子:
HTML代码的屏蔽

Js代码

function mask_HTMLCode(strInput) {

var myReg = /<(\w+)>/;

return strInput.replace(myReg, "<$1>");

}

E-mail address verification:

function test_email(strEmail) {

var myReg = /^[_a-z0-9]+@([_a-z0-9]+.)+[a-z0 -9]{2,3}$/;

if(myReg.test(strEmail)) return true;

return false;

}


Properties and methods of regular expression objects:
Predefined regular expressions The expression has the following static properties: input, multiline, lastMatch, lastParen, leftContext,

rightContext and $1 to $9. Among them, input and multiline can be preset. The values of other attributes are assigned different values according to different conditions after executing the exec or test method. Many properties have both long and short (perl-style) names, and both names refer to the same value. (

JavaScript simulates perl’s regular expressions)

Attributes of regular expression objects:
Attribute meanings
$1...$9 If they (they) exist, they are the matched substrings
$_ See input
$* See multiline
$& See lastMatch
$+ See lastParen
$` See leftContext
$'' See rightContext
constructor Create a special function prototype of an object
global Whether to match in the entire string (bool type)
ignoreC ase When matching Whether to ignore case (bool type)
input    The matched string
lastIndex    The index of the last match
lastParen    The last substring enclosed in parentheses   
leftContext       The substring starting from the left of the most recent match   
multiline   Whether to perform multi-line matching (bool type)
prototype  Allows additional attributes to the object
rightContext  Regular expression pattern   
lastIndex  The index of the last match   Method of regular expression object:
Method meaning
compile  regular expression compare
exec   Execute a search
test   Perform a match
toSource   Return the definition of a specific object (literal

representing), whose value can be used to create a new object. Obtained by overloading the Object.toSource method.
toString   Returns the string of a specific object. Obtained by overloading the Object.toString method.
valueOf   Returns the original value of a specific object. Overload the Object.valueOf method to get


Example:


Js code

will output "Smith, John"

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!