简体   繁体   English

如何在两个字符之间分割字符串?

[英]how to split string between two characters?

How to split target string betweeen /#/ and / chars? 如何在/#//字符之间分割目标字符串?

http://www.site.com/#/target/

The output is supposed to be target . 输出应该是target

Here is a fiddle for the regex required to match the text between the last two slashes: 这是匹配最后两个斜杠之间的文本所需的正则表达式的小提琴:

http://jsfiddle.net/7BGP5/ http://jsfiddle.net/7BGP5/

The code is quite simple: 代码很简单:

var str = "http://www.site.com/#/target/";
var regexp = /.*\/(.*?)\//;
console.log(str.match(regexp)[1]);

I'd suggest to use replace with regular expression: 我建议使用正则表达式replace

"http://www.site.com/#/target/".replace(/.*#\/([^/]+).*/, "$1");
// >> "target"

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

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