简体   繁体   English

提取字符串的中间或最后部分

[英]extracting middle OR final part of a string

I want to extract only the first fontname out of a URL-string from the Google Webfont Directory. 我想从Google Webfont目录中仅提取URL字符串中的第一个字体名称。 Here are some examples of possible strings and what part should be returned: 以下是可能字符串的一些示例以及应返回的部分:

fonts.googleapis.com/css?family=Raleway              // "Raleway"
fonts.googleapis.com/css?family=Caesar+Dressing      // "Caesar Dressing"
fonts.googleapis.com/css?family=Raleway:300,400      // "Raleway"
fonts.googleapis.com/css?family=Raleway|Fondamento   // "Raleway"
fonts.googleapis.com/css?family=Caesar+Dressing|Raleway:300,400|Fondamento  // "Caesar Dressing"

So sometimes it's just one fontname, sometimes it has a weight indicated by a colon ( : ) and sometimes there are more fontnames divided by a pipe ( | ). 所以有时它只是一个字体名称,它也有一个冒号表明其重( : ),有时有更多fontnames通过管道分( | )。

I have tried /family=(\\S*)[:|]/ but it only matches the strings with : or | 我已经试过/family=(\\S*)[:|]/但它只能琴弦用火柴:| . I could do it like this, but it's not a nice solution: 我可以这样做,但这不是一个很好的解决方案:

var fontUrl = "fonts.googleapis.com/css?family=Caesar+Dressing|Raleway:300,400|Fondamento";
var fontName = /family=(\S*)/.exec(fontUrl)[1].replace(/\+/, " ");
if (fontName.indexOf(':') != -1){
    fontName = fontName.split(':')[0];
}
if (fontName.indexOf('|') != -1){
    fontName = fontName.split('|')[0];
}
console.log(fontName);

Is there a nice regex solution to this? 有一个很好的regex解决方案吗?

Instead of matching the character that (might) follow the string you want, match only the string you want except those characters : 而不是匹配(可能)跟随您想要的字符串的字符,只匹配您想要的字符串, 除了这些字符

/family=([^\s:|]*)/

Alternatively, you'd use a lookahead like this: 或者,您可以使用这样的前瞻

/family=(\S*?)(?=$|[:|])/

That should be better: 那会更好:

/family=([^:|]*)/

Of course for the + case, you'll have to replace it afterwards (or before maybe). 当然对于+情况,你必须在之后(或之前)更换它。

尝试这个:

/family\=(\S+?)[\:\|,]{0,2}\S*/ims

You can use (choose the i and m modifier in all case): 你可以使用(在所有情况下选择i和m修饰符):

family=([a-z]+\+?[a-z]+)

or more simply 或者更简单

family=([a-z+]+)

or to avoid matching the + char: 或者避免匹配+ char:

family=([a-z]+)\+?([a-z]+)?

but it is an easyer way to use the second solution, and to replace the + chars with a space after. 但它是一种更简单的方法来使用第二种解决方案,并用后面的空格替换+字符。

No regex is required in this case, unless you are good with regex's or test them thoroughly then you are likely to make mistakes. 在这种情况下不需要正则表达式,除非你对正则表达式有好处或者彻底测试它们,否则你可能会犯错。

var fontUrls = [];

fontUrls.push("fonts.googleapis.com/css?family=Raleway");
fontUrls.push("fonts.googleapis.com/css?family=Caesar+Dressing");
fontUrls.push("fonts.googleapis.com/css?family=Raleway:300,400");
fontUrls.push("fonts.googleapis.com/css?family=Raleway|Fondamento");
fontUrls.push("fonts.googleapis.com/css?family=Caesar+Dressing|Raleway:300,400|Fondamento");

function getFirstFont(url) {
    return url.split("=")[1].split("|")[0].split(":")[0];
}

fontUrls.forEach(function (fontUrl) {
  console.log(getFirstFont(fontUrl));
});

on jsfiddle jsfiddle

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

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