English 中文(简体)
反斜线 - 正则表达式 - 标注
原标题:Backslashes - Regular Expression - Javascript

我想建立一份联署材料功能, 将一系列论据编成一条有效的道路(因为我无法确定这条道路是否有一部分是被划过还是没有被划过) 。

这就是这个函数:

concatPath = function() {
    var path = "";
    for(var i = 0; i < arguments.length; i++)   {
        path += arguments[i].replace("(\|/)$|^(\|/)", "") + "/";
    }
    return path;
}

The used RegEx matched all beginning and ending slashes and backslashes on http://regexpal.com But the function does not work properly (RegEx does not match). Furthermore, Chrome states

SyntaxError: 无效的正则表达式 : /()$_()/()_()/ : 未终结组群

当我刚用那套

 (\)$|^(\)

然而,使用RegEx

 (\)$|^(\)

工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常, 工作正常 工作正常, 工作正常 工作正常 工作正常, 工作正常 工作正常 工作正常 工作正常, 工作正常 工作正常 工作正常 工作正常, 工作正常 工作正常 工作正常 工作正常 工作正常 工作正常 工作正常 工作正常 工作正常 工作正常, 工作正常 工作正常 工作正常 工作正常 工作正常 工作正常, 工作正常 工作正常 工作正常 工作正常 。

是太迟了还是我漏掉了什么特别的东西?

提前感谢!

利奥

最佳回答

您应该使用正则表达式字句( /.../ ) 而不是字符串字句( ... )... 中的字符串字句( replace )... 。字符串在正常表达式构建器破解前对反线有其自身的解释,所以您需要额外引用水平 。

匹配一个反斜线, 正则表达式字句 : /\\/

匹配一个反斜线, 字符串中的正则表达式 : \\\

但从文字上来说,你还必须在正面的鞭笞前 下回鞭笞, 因为前面的鞭笞是整件事的分界线:

path += arguments[i].replace(/(\|/)$|^(\|/)/, "") + "/";

或者,如果因为某种原因与使用字符串结婚,这也应该有效:

path += arguments[i].replace("(\\|/)$|^(\\|/)", "") + "/";

作为侧边注意,当您选择的字符为单字符时, (xy) 是超语法; 您可以只使用字符类( [xy] )。 在这种情况下, 您可以得到以下信息 :

path += arguments[i].replace(/[\/]$|^[\/]/, "") + "/";

path += arguments[i].replace("[\\/]$|^[\\/]", "") + "/";
问题回答

尝试这个... 它比较容易与 [字符类] 匹配。 将单个 < code_ / code> 匹配到您需要的 javascript 字符串 < code_ / code >, 这可能是正在发生的事情 。

new Regexp (___[\\\/]\\\\\__[\\/]$]

您也可以尝试 /\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\co>的符号符号符号符号符号。

s =  c:\folder\ 
console.log(s.replace(/^[\/]|[\/]$/g,   ))




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.