简体   繁体   English

如何在javascript中使用空格转义字符

[英]How to escape characters in javascript with a space before

I would like to escape a " /" (a slash with a space before ) in a javascript expression. 我想在JavaScript表达式中转义“ /”( 在之前有一个空格的斜杠)。

Naturally, it works with this expression: 自然,它可以使用以下表达式:

string.replace(/\//g,"\n");

It replaces all the "/" -s in my string with a "\\n" (console linebreak). 它将字符串中的所有“ /”- s替换为“ \\ n” (控制台换行符)。

But what I really need is an expression, or a method to replace " /"-s instead of "/"-s . 但是,我真正需要的是一个表达式或一种替换“ /”-s而不是“ /”-s的方法

As You may know, unfortunatelly the escaping expression breaks this way: 如您所知,不幸的是,转义表达式打破了这种方式:

string.replace(/\ //g,"\n");

Thanks for Your help! 谢谢你的帮助!

You just need: 您只需要:

string.replace(/ \//g,"\n");

so that you're matching a space followed by an (escaped) forward slash. 因此您要匹配一个空格,后跟一个(转义的)正斜杠。

This has some good examples: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 这有一些很好的例子: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

这应该工作正常:

string.replace(/\s\//g,"\n");

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

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