replace
语法 stringObj.replace(rgExp, replaceText)
stringObj 必选项。要执行该替换的 String 对象或文字。该对象不会被 replace 方法修改。
rgExp 必选项。描述要查找的内容的一个正则表达式对象。
replaceText 必选项。是一个String 对象或文字,对于stringObj 中每个匹配 rgExp 中的位置都用该对象所包含的文字加以替换。
例如:
Js代码
<script> </script>
var strM = "javascript is a good script language";
//在此我想将字母a替换成字母A
alert(strM.replace("a","A"));
这样只能替换第一个“a”字母
<script> </script>
var strM = "javascript is a good script language";
//在此将字母a全部替换成字母A
alert(strM.replace(/a/g,"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 = //;
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

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools
