[英]JavaScript get the last character's position of the string inside the other string
i guess the problem is extremely casual, but can't really find the solution. 我猜这个问题是很随意的,但是找不到真正的解决方案。
i have a string 我有一个弦
var full = 'hopaopGmailb3GkI'
var name = 'hopaop'
var service = 'Gmail'
How do i get the b3GkI
from the original string , suppose the name and service are dynamic values also parsed from the string var full
. 我如何从原始字符串中获取
b3GkI
,假设名称和服务也是从字符串var full
解析出来的动态值。
Or how to get the index of the last character of the variable service
inside of the var full
string 或者如何获取
var full
字符串内的变量service
的最后一个字符的索引
I need to get the index of the last character of the variable service
inside of the var full
string. 我需要获取
var full
字符串内变量service
的最后一个字符的索引。 var index = full.indexOf(service);
returning the first character's index (6 in this example). 返回第一个字符的索引(在此示例中为6)。 Also for some reason i can't exclude name + service from the string.
同样由于某种原因,我不能从字符串中排除名称+服务。
var index = full.indexOf(service) + service.length;
should do the trick. 应该可以。 I also feel like you can come up with a regular expression to accomplish this, but I am no master.
我也觉得您可以提出一个正则表达式来完成此操作,但我不是高手。
Version 1 版本1
var full = 'hopaopGmailb3GkI'; var name = 'hopaop'; var service = 'Gmail'; var result = full.substring(full.indexOf(service) + service.length) console.log(result);
Version 2 版本2
var full = 'hopaopGmailb3GkI'; var name = 'hopaop'; var service = 'Gmail'; var result = full.split(service)[1]; console.log(result);
Another alternative with Regular Expressions 正则表达式的另一种选择
var full = 'hopaopGmailb3GkI';
var service = 'Gmail';
console.log((function(source, service) {
var match = new RegExp('(.+)' + service + '(.+)').exec(source);
return {
'service': service,
'name': match[1],
'token': match[2]
};
})(full, service));
Returns an object like {"service":"Gmail","name":"hopaop","token":"b3GkI"}
返回一个对象,例如
{"service":"Gmail","name":"hopaop","token":"b3GkI"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.