简体   繁体   中英

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.

Naturally, it works with this expression:

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

It replaces all the "/" -s in my string with a "\\n" (console linebreak).

But what I really need is an expression, or a method to replace " /"-s instead of "/"-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

这应该工作正常:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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