1
IHAVE當我使用js.I一個問題有一個字符串=>"c:0.1|d:0.2"
,我需要這樣的=>c:10%,d:20%
如何通過angularjs拆分字符串?
IHAVE當我使用js.I一個問題有一個字符串=>"c:0.1|d:0.2"
,我需要這樣的=>c:10%,d:20%
如何通過angularjs拆分字符串?
使用String#split
,Array#map
和Array#join
方法輸出。
var str = "c:0.1|d:0.2";
console.log(
str
// split string by delimitter `|`
.split('|')
// iterate and generate result string
.map(function(v) {
// split string based on `:`
var s = v.split(':')
// generate the string
return s[0] + ':' + (Number(s[1]) * 100) + "%"
})
// join them back to the format
.join()
)
您還可以使用String#replace
法capturing group regex和一個回調函數。
var str = "c:0.1|d:0.2";
console.log(
str.replace(/\b([a-z]:)(0\.\d{1,2})(\|?)/gi, function(m, m1, m2, m3) {
return m1 +
(Number(m2) * 100) + // calculate the percentage value
(m3 ? "%," : "%") // based on the captured value put `,`
})
)
這不是角度問題,您可以以後用簡單的邏輯,用串取值爲:再乘以100得到的數值爲10或相應。
非常感謝你O(∩_∩)O –