简体   繁体   English

从img src属性中提取字符串?

[英]Extract string from img src attribute?

How could i take fontsize, fonttext and fonttype value from following img src 我如何从以下img src中获取fontsize,fonttext和fonttype值

<img 
  src="bin/contenthandler.php?fontsize=36&fonttext=apple&fonttype=fonts/FOO.ttf"
  class="selected content resizable">

I think it can be done with regular expressions but I am bad with them. 我认为可以用正则表达式来完成,但是我对它们不好。

Doing it with the location object would be ideal, avoids all the troublesome regex. 最好使用location对象,避免所有麻烦的正则表达式。 Borrowed from: Parse URL with jquery/ javascript? 借用自: 使用jquery / javascript解析URL? and https://developer.mozilla.org/en/window.location https://developer.mozilla.org/en/window.location

function buildValue(sValue) {  
    if (/^\s*$/.test(sValue)) { return(null); }  
    if (/^(true|false)$/i.test(sValue)) { return(sValue.toLowerCase() === "true"); }  
    if (isFinite(sValue)) { return(parseFloat(sValue)); }  
    if (isFinite(Date.parse(sValue))) { return(new Date(sValue)); }  
    return(sValue);  
}      
function getVars(url) {
    var oGetVars = {};  
    var a = document.createElement('a');
    a.href = url;
    var iCouple, aCouples = a.search.substr(1).split("&");  
    for (var iCouplId = 0; iCouplId < aCouples.length; iCouplId++) {  
        iCouple = aCouples[iCouplId].split("=");  
        oGetVars[unescape(iCouple[0])] = iCouple.length > 1 ?     buildValue(unescape(iCouple[1])) : null;  
    }  
    return oGetVars;
}

console.log(getVars('http://google.com?q=123&y=xyz'));  

This will return an object with all the variables of the query. 这将返回带有查询的所有变量的对象。
jsFiddle Demo jsFiddle演示

Despite the other two answers, here is an alternative, 尽管有其他两个答案,这是另一种选择,

$('img[src]').each(function (i,n){
   var item  = $(n).attr('src');
   var query = item.split('?');
   var items = query.split('&') ;
   // so now you get the point, u split each item again by the "=" sign :) this is reusable     
   // provided you put it on a function, and it can search to return a specific one, with a little imagination :) 
});

Another alternative is by using the URI library. 另一种选择是使用URI库。

I have used this numerous times and you get exactly what you want, and get everything to do with URI/URL manipulations. 我已经使用了无数次,您可以确切地得到所需的内容,并且可以使用URI / URL操作进行所有操作。

http://medialize.github.io/URI.js/ http://medialize.github.io/URI.js/

$('img[src]').each(function (i,n){
   var src  = $(n).attr('src');
   var get_query = URI(src).query()
   console.log(get_query)
});

here are some examples... 这里有些例子...

URI("testme?test").query();// returns: test
URI("testme?a=1&b=2").query(true) // returns: {a: "1", b: "2"}
URI("testme?font_size=15&font_name=arial").query(true).font_size // 15

more information about how to use ... click here http://medialize.github.io/URI.js/docs.html#accessors-search 有关如何使用的更多信息...请单击此处http://medialize.github.io/URI.js/docs.html#accessors-search

This will get you the value 36 . 这将为您提供值36

var rx = /fontsize=(.*?)&/;
var fontsize = rx.exec('<img src="bin/contenthandler.php?fontsize=36&fonttext=apple&fonttype=fonts/FOO.ttf" class="selected content resizable">')[1];

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

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