![](/img/trans.png)
[英]how to remove a certain character at the end of the string in javascript
[英]In a dynamic string, how to start a substring at a certain character and end it at certain character?
我在javascript中有字串。
var string = "BLAH BLAH BLAH width: 673px; height: 123px; BLAH BLAH BLAH width: 1009px; height: 237px; BLAH BLAH"
我想從中得到兩個子串。 一個以'width'
開頭並以'px'
結尾,另一個以'height'
開頭並以'px'
結尾。
每對'width'
和'height'
都會發生這種情況。 這可能在我的字符串中出現n次。 每次用不同的數字表示寬度和高度。
我真的不知道該怎么做,因為字符串的長度是動態的。 我不能只是在某個索引處開始一個substring
或substr
並轉到某個索引。 我看不到如何使用split
和join
。 無論如何,使用jQuery或javascript可以做到這一點。
我目前的方式很慢...
for (var w = 1; w <= 10000; w++) {
for(var h = 1; h<=10000; h++)
{
var match = text.match("style=\"width: " + w + "px; height: " + h + "px;");
if (match != null) {
text = text.split("style=\"width: " + w + "px; height: " + h + "px;").join('');
}
}
}
找到子字符串后,我希望將其從我的字符串中刪除。
編輯后,您可以根據需要簡單地執行此操作:
var string = "BLAH BLAH BLAH width: 673px; height: 123px; BLAH BLAH BLAH width: 1009px; height: 237px; BLAH BLAH";
var regExp = /(\b\w+:[^:]*px)/g
string = string.replace(regExp,'')
使用正則表達式,您可以這樣:
var string = "BLAH BLAH BLAH width: 673px; height: 123px; BLAH BLAH BLAH width: 1009px; height: 237px; BLAH BLAH";
var regExp = /(\b\w+:[^:]*px)/g
var match = regExp.exec(string);
while(match != null)
{
var width = match[1]
match = regExp.exec(string);
var height = match[1]
match = regExp.exec(string);
}
此后,在每次迭代中,變量width
將具有子字符串width: 4874px
,變量height
將具有子字符串height: 898px
提取其他兩個字符串之間的一種優雅方法是向String對象添加一個函數。
/** * Extract a string between a Prefix and a Suffix * * @param prefix {string} Prefix string * @param suffix {string} Suffix string * @param trimSpace {boolean} (optional default false) is string space trimmed * @returns {string} String between prefix and suffix */ String.prototype.between = function(prefix, suffix, trimSpace) { trimSpace = typeof trimSpace !== 'undefined' ? trimSpace : false; var thisString = this; var index = thisString.indexOf(prefix); if (index >= 0) { thisString = thisString.substring(index + prefix.length); } else { return ""; } if (suffix) { index = thisString.indexOf(suffix); if (index < 0) { return ""; } else { thisString = thisString.substring(0, index); } } return trimSpace ? thisString.trim() : thisString; } var theString = "*Other text that is dynamic in length* width: 4874px; height: 898px *more dynamic length text*" var width = theString.between("width:", "px"); var widthTrimmed = theString.between("width:", "px", true); var heightTrimmed = theString.between("height:", "px", true); document.write("Not trimmed width: width=>" + width + "<" + "<br/>"); document.write("Trimmed width: widthTrimmed=>" + widthTrimmed + "<" + "<br/>"); document.write("Trimmed height: heightTrimmed=>" + heightTrimmed + "<" + "<br/>");
<body></body>
假設格式保持不變,則獲取寬度/高度和px的索引應該足夠
var string = "*Other text that is dynamic in length* width: 4874px; height: 898px; *more dynamic length text*"; var windexStart=string.indexOf('width')+6; var windexStop=string.indexOf('px;'); var hindexStart=string.indexOf('height')+7; var hindexStop=string.lastIndexOf('px;'); var width=string.substring(windexStart,windexStop); var height=string.substring(hindexStart,hindexStop); alert(width+' '+height);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.