目前我从this api获取代码 - 我试图以用户友好的方式显示车辆列表,也就是'类名'和我& #39;使用下面的代码运行正确:
var vehicleList = data.vehicles.map(v => v.classname);
var content = vehicleList.join(", ");
document.getElementById("vehicle").textContent = content;
但是,我想用更易理解的车辆替换车辆名称,例如API可能会说" B_Quadbike_01"当我真的想要它只是说" Quadbike"在我的HTML中。我已尝试使用以下代码执行此操作:
content.replace(/B_Quadbike_01/ig, "Quadbike");
然而,它只是不起作用 - 我做得对吗?您可以通过车辆列表over here看到我的意思,它看起来像是一团糟。
答案 0 :(得分:2)
replace
没有处理它返回结果的对象
你应该像这样复制它:
content = content.replace(/B_Quadbike_01/gi, "Quadbike");
答案 1 :(得分:1)
使所有类可读,例如所有包含 3 个字符的单词
string.match(/(?:[A-Za-z]{3,})/g)
var data = {"uid":169243,"name":"PC Ant Arni","steamid":"76561198062083666","forumname":null,"forumid":null,"forumurl":null,"steam":{"name":"Ant Arni","profileurl":"https:\/\/steamcommunity.com\/id\/Antarni\/","avatarSmall":"https:\/\/steamcdn-a.akamaihd.net\/steamcommunity\/public\/images\/avatars\/25\/25f4a505cbce0a9b1deacb7468e18898bcff484f.jpg","avatarMedium":"https:\/\/steamcdn-a.akamaihd.net\/steamcommunity\/public\/images\/avatars\/25\/25f4a505cbce0a9b1deacb7468e18898bcff484f_medium.jpg","avatarLarge":"https:\/\/steamcdn-a.akamaihd.net\/steamcommunity\/public\/images\/avatars\/25\/25f4a505cbce0a9b1deacb7468e18898bcff484f_full.jpg"},"guid":"33b56e488b3d41d67ff3bc1f57b3d101","cash":0,"bank":11048411,"adminlevel":0,"wanted":false,"groupid":204,"grouprank":3,"groupname":"The Copper Kings","unmcrank":0,"unmcair":0,"unmcranger":0,"unmclastlogin":null,"policerank":3,"npasrank":0,"mporank":2,"arrank":0,"ncarank":0,"rturank":1,"undercoverrank":"0","policelastlogin":"2017-12-03 19:19:15","nhsrank":4,"nhsarrank":0,"nhsrirrank":1,"nhslastlogin":"2017-11-05 21:27:15","civ_licenses":[{"license":"driver","owned":true},{"license":"air","owned":true},{"license":"heroin","owned":true},{"license":"marijuana","owned":true},{"license":"boat","owned":true},{"license":"oil","owned":true},{"license":"dive","owned":true},{"license":"truck","owned":true},{"license":"gun","owned":true},{"license":"rebel","owned":true},{"license":"coke","owned":true},{"license":"diamond","owned":true},{"license":"copper","owned":true},{"license":"plastic","owned":true},{"license":"aluminium","owned":true},{"license":"silica","owned":true},{"license":"iron","owned":true},{"license":"sand","owned":true},{"license":"salt","owned":true},{"license":"cement","owned":true},{"license":"home","owned":true},{"license":"event","owned":false},{"license":"meth","owned":true},{"license":"taxi","owned":true},{"license":"arac","owned":true},{"license":"rebelimports","owned":true}],"vehicles":[{"id":360502,"classname":"B_Quadbike_01_F","type":"Car","color":7},{"id":360505,"classname":"C_Offroad_01_F","type":"Car","color":4},{"id":364532,"classname":"C_Van_01_box_F","type":"Car","color":1},{"id":367013,"classname":"C_Kart_01_Vrana_F","type":"Car","color":0},{"id":373584,"classname":"O_MRAP_02_F","type":"Car","color":0},{"id":386845,"classname":"C_Hatchback_01_sport_F","type":"Car","color":0},{"id":392893,"classname":"C_Scooter_Transport_01_F","type":"Ship","color":3},{"id":393490,"classname":"B_Truck_01_box_F","type":"Car","color":0},{"id":398146,"classname":"O_Plane_CAS_02_F","type":"Air","color":0},{"id":402207,"classname":"B_Truck_01_box_F","type":"Car","color":0},{"id":402335,"classname":"O_LSV_02_unarmed_F","type":"Car","color":1},{"id":407171,"classname":"C_Scooter_Transport_01_F","type":"Ship","color":4},{"id":414610,"classname":"C_Plane_Civil_01_racing_F","type":"Air","color":0},{"id":414696,"classname":"C_Hatchback_01_sport_F","type":"Car","color":3},{"id":415938,"classname":"C_Plane_Civil_01_racing_F","type":"Air","color":0},{"id":415939,"classname":"I_Heli_light_03_unarmed_F","type":"Air","color":2},{"id":419203,"classname":"I_C_Offroad_02_unarmed_F","type":"Car","color":5},{"id":420228,"classname":"B_T_VTOL_01_infantry_F","type":"Air","color":0},{"id":420230,"classname":"B_Heli_Transport_03_unarmed_F","type":"Air","color":0},{"id":420248,"classname":"I_Truck_02_covered_F","type":"Car","color":0},{"id":427634,"classname":"O_MRAP_02_F","type":"Car","color":0},{"id":427635,"classname":"O_Heli_Light_02_unarmed_F","type":"Air","color":3},{"id":427640,"classname":"C_Hatchback_01_sport_F","type":"Car","color":0},{"id":427641,"classname":"O_Heli_Transport_04_bench_F","type":"Air","color":0},{"id":429865,"classname":"I_Truck_02_fuel_F","type":"Car","color":0},{"id":438410,"classname":"C_Plane_Civil_01_racing_F","type":"Air","color":0},{"id":445053,"classname":"B_Heli_Light_01_F","type":"Air","color":14},{"id":445054,"classname":"B_Heli_Light_01_F","type":"Air","color":3},{"id":445857,"classname":"B_T_VTOL_01_infantry_F","type":"Air","color":0},{"id":447813,"classname":"C_Van_02_vehicle_F","type":"Car","color":7},{"id":447814,"classname":"C_Van_02_transport_F","type":"Car","color":7},{"id":448115,"classname":"I_G_Van_02_transport_F","type":"Car","color":0},{"id":448119,"classname":"I_G_Van_02_vehicle_F","type":"Car","color":0},{"id":453900,"classname":"C_Van_01_fuel_F","type":"Car","color":0},{"id":458437,"classname":"O_Plane_Fighter_02_Stealth_F","type":"Air","color":0}]}
var vehicleList = data.vehicles.map(v => v.classname.match(/(?:[A-Za-z]{3,})/g).join("-"));
var content = vehicleList.join(", ");
document.getElementById("vehicle").textContent = content;
<div id="vehicle"></div>