<jgallardo949>
author
的div内生成的字符串的每个实例执行此操作"contributed_by":"Sasha <SashaMasondeCaires>"
Sasha <a href="https://www.twitter.com/SashaMasondeCaires">@SashaMasondeCaires</a>
由于当前返回的格式是name <twitter username>
,所以我要做的是将<
替换为<a href="https://www.twitter.com/
,将>
替换为</a>
,但是那我应该怎么处理这个名字,也许把那个变量变成一个类似
//To get the string inside the <>
var authTw = document.getElementsByClassName("author")[0].innerHTML;
//wrapping solution needs work
var twUsername = "<span>@" + authTw.match(/\<([a-z]*)\>/)[1] + "</span>"
document.getElementsByClassName("author")[0].innerHTML=twUsername;
var authorName = // I need to get the string before the < symbol
var contributorInfo = authorName + '<a href="https://twitter.com/' + twUsername + '">' + twUsername + '</a>';
然后,我需要使用类contributorInfo
在div中写入结果.author
<div class="author">{{ beer.contributor }}</div>
contributor: api.contributed_by,
https://api.punkapi.com/v2/beers/random
[{
"id": 126,
"name": "Jet Black Heart",
"tagline": "Oatmeal Milk Stout. Dark. Roasty. Velvety.",
"first_brewed": "01/2016",
"description": "Good things come to those who wait. This smooth and roasty oatmeal milk stout won our 2015 Prototype Challenge at a canter. Roasty coffee and chocolate lead to a decadent, full-bodied richness of near uncharted depths with a velvet mouthfeel from the addition of oatmeal and a touch of wheat. This is complemented at every turn by the Magnum and Sorachi Ace hops, with the latter bringing an intensity of smooth vanilla and dark berry fruit on the long, rewarding finish.",
"image_url": "https://images.punkapi.com/v2/126.png",
"abv": 4.7,
"ibu": 45,
"target_fg": 1019,
"target_og": 1055,
"ebc": 200,
"srm": 100,
"ph": 4.4,
"attenuation_level": 70,
"volume": {
"value": 20,
"unit": "liters"
},
"boil_volume": {
"value": 25,
"unit": "liters"
},
"method": {
"mash_temp": [{
"temp": {
"value": 65,
"unit": "celsius"
},
"duration": 75
}],
"fermentation": {
"temp": {
"value": 19,
"unit": "celsius"
}
},
"twist": null
},
"ingredients": {
"malt": [{
"name": "Pale Ale",
"amount": {
"value": 2.75,
"unit": "kilograms"
}
}, {
"name": "Wheat",
"amount": {
"value": 0.25,
"unit": "kilograms"
}
}, {
"name": "Dark Crystal",
"amount": {
"value": 0.19,
"unit": "kilograms"
}
}, {
"name": "Brown",
"amount": {
"value": 0.38,
"unit": "kilograms"
}
}, {
"name": "Black",
"amount": {
"value": 0.19,
"unit": "kilograms"
}
}, {
"name": "Carafa Special Malt Type 1",
"amount": {
"value": 0.19,
"unit": "kilograms"
}
}, {
"name": "Flaked Oats",
"amount": {
"value": 0.38,
"unit": "kilograms"
}
}, {
"name": "Crystal 150",
"amount": {
"value": 0.25,
"unit": "kilograms"
}
}, {
"name": "Lactose",
"amount": {
"value": 0.38,
"unit": "kilograms"
}
}],
"hops": [{
"name": "Magnum",
"amount": {
"value": 12.5,
"unit": "grams"
},
"add": "start",
"attribute": "bitter"
}, {
"name": "Sorachi Ace",
"amount": {
"value": 6.3,
"unit": "grams"
},
"add": "middle",
"attribute": "flavour"
}],
"yeast": "Wyeast 1056 - American Ale™"
},
"food_pairing": ["Oyster beignets", "Beef shin stew", "A Shakin' jesse"],
"brewers_tips": "There's a lot of speciality malt in the mash. Make sure you take the run off nice and steady – increase the flow too much and pull in the bed at your peril.",
"contributed_by": "Sasha <SashaMasondeCaires>"
}]
一个答案有所帮助,这是my current pen
由于我多次使用author
类向divs写入字符串
<div class="author">Joe <crabshack></div>
<div class="author">juan <tacotruck></div>
<div class="author">Jesse <Canvas></div>
var user = document.getElementsByClassName('author').innerHTML;
var matches = user.match(/(.*)\s\<(.*)\>/);
var output = `${matches[1]} <a href="https://www.twitter.com/${matches[2]}" target="_blank">@${matches[2]}</a>`;
document.body.innerHTML = output;
问题所在的部分是var user = document.getElementsByClassName('author').innerHTML;
当我尝试
var user = document.querySelector(".author").innerHTML;
这只是给我第一堂课,但我需要获取所有值。
答案 0 :(得分:1)
以下是使用JavaScript template literals ${variable}
和正则表达式/(.*)\s\<([a-z]*)\>/
的方法。
堆栈片段
var user = "Sasha <SashaMasondeCaires>";
var matches = user.match(/(.*)\s\<(.*)\>/);
var output = `${matches[1]} <a href="https://www.twitter.com/${matches[2]}">@${matches[2]}</a>`;
document.body.innerHTML = output;
基于comment / jsfiddle演示进行了更新
这是您的演示代码的更新版本
var user = document.getElementsByClassName('author');
for (var i = 0; i < user.length; i++) {
var matches = user[i].innerHTML.match(/(.*)\s\<([a-z]*)\>/);
var output = `${matches[1]} <a href="https://www.twitter.com/${matches[2]}" target="_blank">@${matches[2]}</a><br>`;
document.body.innerHTML += output;
}
.author {
display: none;
}
<div class="author">Joe <crabshack></div>
<div class="author">juan <tacotruck></div>
<div class="author">Joe <Canvas></div>
<div id="author">Juan Gallardo <JGallardo949></div>
答案 1 :(得分:0)
我想最容易解析的就是:
const [name, twitter] = input.split(">")[0].split("<").map(str => str.trim());
return `${name} <a href="twitter.com/${twitter}" >@${twitter}</a>`;