javascript replace函数
定义
replace()函数用于替换字符串.
语法
stringObject.replace(regexp/substr, newSubStr/function)
参数
regexp/substr 正则表达式/字符串
newSubStr/function 替换文本/生成替换文本的函数
示例
var str = "Visit Microsoft!"; var res = str.replace("Microsoft", "W3Schools");
替换字符
假如替换字符newSubStr中包含$符号,它表示什么?
$1、 $2、 ...、$99 与 regexp 中的第1到第99个子表达式相匹配的文本
$& 与 regexp 相匹配的子串
$` 位于匹配子串左侧的文本
$' 位于匹配子串右侧的文本
$$ 直接量符号。
上面$1、$2...中提到的子表达式是什么意思?正则表达式中用小括号包裹起来的表达式.
例如:
/a/ 无子表达式
/[a]/ 无子表达式
/(a)/ 有子表达式(a)
/(a), (b)/ 有子表达式(a)、(b)
示例
name = "Doe, John"; name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1"); //输出"John Doe"
替换函数
替换函数说明
function(match, p1, p2,..., offset, string)
match 匹配到的字符串
p1, p2... 匹配到的子表达式
offset 匹配到字符串的位置
string 整个字符串
示例
var name = "ace:mm-Abc".replace(/([\:\-\_]+(.))/, function(match, p1, p2, offset, total){ console.log("match:" + _); console.log("p1:" + separator); console.log("p2:" + letter); console.log("offset:" + offset); console.log("total:"+ total) return offset ? p2.toUpperCase() : p2; }) console.log(name) /*输出 substr::m test.html:43 p1::m p2:m offset:3 total:ace:mm-Abc aceMm-Abc */
上面正则表达式中有两个子表达式
p1是子表达式([\:\-\_]+(.))匹配到的内容":m"
p2是子表单是(.)匹配到的内容"m"
2023年6月15日 22:55
At CertsQuestions, we believe that everyone should have access to high-quality study materials to help them prepare for their certification exam. That's why we offer our NSE6_FAC-6.4 PDF Dumps at an affordable price, making them accessible to everyone.
2023年6月15日 22:57
At CertsQuestions, we believe that everyone should have access to high-quality study materials to help them prepare for their certification exam. That's why we offer our NSE6_FAC-6.4 PDF Dumps at an affordable price, making them accessible to everyone.
2023年6月15日 22:58
If you're looking for the best and most accurate Change-Management-Foundation PDF Exam Dumps to help you prepare for your certification exam, look no further than CertsQuestions. We are committed to providing our customers with the best possible study materials to help them succeed in their certification journey.
2023年6月15日 22:59
CertsQuestions study materials are designed to be easy to use and understand, accurate and up-to-date, and affordable and accessible. We also offer a variety of customer support options to help you prepare for your PEGACPSA88V1 Exam Dumps and succeed.
2023年9月22日 21:03
Dumps4Expert often provides guarantees for MB-800 pdf exam dumps that if you diligently follow their study plan and still do not pass your exam, they will refund your money.
2023年9月22日 21:05
You can start by exploring the materials and reviews on their website. Additionally, you can contact their customer support for guidance on selecting the proper PSPO-III exam dumps for your certification goals.
2023年9月22日 21:06
Dumps4Expert offers a wide range of DP-203 exam dumps covering various certifications, making them suitable for a diverse audience.
2023年9月22日 21:07
Discover how to achieve success with the latest Dumps4Expert 350-401 pdf exam dumps. Explore expert insights, FAQs, and valuable information for your exam preparation journey.
2023年9月22日 21:07
Of course, Dumps4Expert can assist you in SASM PDF Dumps. How can I help you with Dumps4Expert SASM exam dumps? If you have any questions or need information about specific exams or study materials, feel free to ask.
2024年2月29日 20:50
Certs4Exam is a game-changer in the certification realm. The wealth of resources and expert guidance ensures success in exams. Kudos to the team!
2024年2月29日 20:54
Navigating the 200-301 Exam Questions certification journey became a breeze with Certs4Exam. The comprehensive study materials and user-friendly interface make it a top choice.
2024年2月29日 20:55
Navigating the 200-301 Exam Questions certification journey became a breeze with Certs4Exam. The comprehensive study materials and user-friendly interface make it a top choice.
2024年7月18日 17:08
DCFC PDF Dumps provides top-notch exam dumps for all your certification needs.