如何在问号后删除字符串?

时间:2013-07-08 04:43:48

标签: javascript

我有一堆格式相似的网址,所以这里是示例网址:

http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg?818629
http://safebooru.org//samples/567/sample_15b9d81bb58b540a6515c1d61cf54f6b7e38d5ab.jpg?569623

我想要做的是删除后的文字?包括?所以输出应该是这样的:

http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg
http://safebooru.org//samples/567/sample_15b9d81bb58b540a6515c1d61cf54f6b7e38d5ab.jpg

你怎么能用javascript做到这一点?

3 个答案:

答案 0 :(得分:0)

http://www.w3schools.com/jsref/jsref_split.asp

var str="How are you doing today?";
var n=str.split(" ");

结果:

如何,是,你在做,今天?

在你的情况下:

var url = 'http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg?818629';
var array = url.split("?");
var myNewUrl = array[0];

答案 1 :(得分:0)

网址:http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg?818629

正则表达式:.*\?

匹配:http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg

测试here

答案 2 :(得分:0)

var string = 'http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg?818629';

var modified = string.replace(/\?.*$/, '');

console.log(modified)

// displays
// http://safebooru.org//images/813/a8b349b60d0c448eb86cfb99e24318ad6d48b7df.jpg