Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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 在url中查找字符串位置并替换url_Javascript_Html - Fatal编程技术网

Javascript 在url中查找字符串位置并替换url

Javascript 在url中查找字符串位置并替换url,javascript,html,Javascript,Html,假设我现在在: http://example.com/page/1/foo/something/here 因此,我需要做的是搜索window.location.href中的特定字符串,在本例中为foo,我需要在字符串foo之前搜索url,因此在本例中为 http://example.com/page/1/ 我怎样才能做到这一点呢?有很多方法。以下是最短的: 'http://example.com/page/1/foo/something/here'.split('foo')[0] 有很多方

假设我现在在:

http://example.com/page/1/foo/something/here
因此,我需要做的是搜索window.location.href中的特定字符串,在本例中为foo,我需要在字符串foo之前搜索url,因此在本例中为

http://example.com/page/1/

我怎样才能做到这一点呢?

有很多方法。以下是最短的:

'http://example.com/page/1/foo/something/here'.split('foo')[0]

有很多方法。以下是最短的:

'http://example.com/page/1/foo/something/here'.split('foo')[0]