如何使用jquery或javascript从CSSTEXT获取价值

时间:2014-10-02 20:46:47

标签: javascript jquery regex string

var text = 'cssText: "@font-face { font-family: myFirstFont; src: url(http://fiddle.jshell.net/hiteshbhilai2010/dpfpLyq1/17/show/sansation_light.woff); }"';

如何从此获取src: url(http://fiddle.jshell.net/myfiddle/dpfpLyq1/17/show/sansation_light.woff);值。

我想获取source和fontFamily值并将其存储在我的js变量中。 fontSrc = url(http://fiddle.jshell.net/myfiddle/dpfpLyq1/17/show/sansation_light.woff)

fontName = 'myFirstFont'

可以帮助一些人

由于

1 个答案:

答案 0 :(得分:2)

您可以使用:

var url = (text.match(/src: *url[^;]+;/i) || [""])[0];
//=> src: url(http://fiddle.jshell.net/hiteshbhilai2010/dpfpLyq1/17/show/sansation_light.woff);

var ff = (text.match(/font-family: *([^;]+);/i) || ["", ""])[1];
//=> myFirstFont