I have a string
var str="java+script+regex";
now i want to split the regex by +
symbol only when +
comes after word java.
So when i try
str.split(/java(\+)/)
then it splits the string using java+.So how can i split it using the +
which comes after word java getting the result ["java","script+regex"]
You can use split captures:
"java+script+regex".split(/(java)\+/)
which gives you
["", "java", "script+regex"]
or match
:
"java+script+regex".match(/(java)(?:\+)(.+)/).slice(1)
["java", "script+regex"]
or replace
:
"java+script+regex+java+chrome".replace(/(java)\+/g, "$1@").split("@")
["java", "script+regex+java", "chrome"]
This post shows different ways to emulate lookbehinds in javascript.
Simply do this
var n=(" "+str).split(/(?=java[+])/)
.join("java")
.split(/java[+]/);
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.