Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/420.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从两侧提取字符串和相等数量的字符_Javascript - Fatal编程技术网

Javascript 从两侧提取字符串和相等数量的字符

Javascript 从两侧提取字符串和相等数量的字符,javascript,Javascript,我有这根绳子 var string = "Economists estimate India to have been the most populous and wealthiest region of the world throughout the first millennium CE. This advantage was lost in the 18th century as other regions edged forward"; 和一个变量var exp=“lost”我想在变

我有这根绳子

var string = "Economists estimate India to have been the most populous and wealthiest region of the world throughout the first millennium CE. This advantage was lost in the 18th century as other regions edged forward";

和一个变量
var exp=“lost”我想在变量word lost的左右两侧各输入10个字符。我如何才能做到这一点?

您可以使用方法和方法的组合来实现这一点

var string=“经济学家估计,在公元前一千年,印度一直是世界上人口最多、最富裕的地区。随着其他地区的发展,这一优势在18世纪消失了”;
var exp=“丢失”;
var index=string.indexOf(exp);
var leftIndex=指数-10;
var rightIndex=指数+10;
var result=string.substring(leftIndex,righindex+exp.length);
警报(结果)带有

substr()
方法返回从指定位置开始到指定字符数的字符串中的字符

var string=“经济学家估计,在公元前一千年,印度一直是世界上人口最多、最富裕的地区。随着其他地区的发展,这一优势在18世纪消失了”,
exp=“丢失”,
part=string.substr(string.indexOf(exp)-10,20+exp.length);
文件。编写(部分)