简体   繁体   English

Javascript替换函数说明

[英]Javascript replace function clarification

I am trying to learn javascript i have this code: 我正在尝试学习javascript我有这个代码:

x=x.replace(/^\s+|\s+$/g,"");

can i have a description about /^\\s+|\\s+$/g,"" 我能描述一下/ ^ \\ s + | \\ s + $ / g,“”

searching to replace what with what ? 寻找替换什么?

Regards 问候

This is a regular expression . 这是一个正则表达式 Basically \\s matches whitespac characters and replaces them with "" . 基本上\\s匹配whitespac字符并用""替换它们。

edit: / ... / marks the regex. 编辑: / ... /标记正则表达式。

^\\s+ Take 1or more whitespaces at the beginning of the string. ^\\s+在字符串的开头加一个或多个空格。

\\s+$ Take 1 or more whitespaces at the end of the string. \\s+$在字符串末尾取一个或多个空格。

/g Dont stop at the first match, but find all matches - "global flag" /g不要在第一场比赛停止,但找到所有比赛 - “全球旗帜”

It's removing whitespaces, either at the beginning or the end of the string, by replacing them with the empty string "" . 它通过用空字符串""替换它们来删除字符串的开头或结尾处的空格。

You can find more about regular expressions here http://www.w3schools.com/jsref/jsref_obj_regexp.asp . 您可以在http://www.w3schools.com/jsref/jsref_obj_regexp.asp找到有关正则表达式的更多信息。

首先,看看这个: http//www.w3schools.com/jsref/jsref_replace.asp然后不要使用stackoverflow来解决这类问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM