简体   繁体   English

在动态字符串中,如何在特定字符处开始子字符串并在特定字符处结束子字符串?

[英]In a dynamic string, how to start a substring at a certain character and end it at certain character?

I have string in javascript. 我在javascript中有字串。

var string = "BLAH BLAH BLAH width: 673px; height: 123px; BLAH BLAH BLAH width: 1009px; height: 237px; BLAH BLAH"

I want to have two substrings come out of this. 我想从中得到两个子串。 One that starts at 'width' and end at 'px' and one that starts at 'height' and ends at 'px' . 一个以'width'开头并以'px'结尾,另一个以'height'开头并以'px'结尾。

This will happened for each pair of 'width' and 'height' . 每对'width''height'都会发生这种情况。 This can appear n amount of times in my string. 这可能在我的字符串中出现n次。 Each time with different numbers for width and height. 每次用不同的数字表示宽度和高度。

I really don't know how to go about this since the string is dynamic in length. 我真的不知道该怎么做,因为字符串的长度是动态的。 I can't just start a substring or substr at a certain index and go to a certain index. 我不能只是在某个索引处开始一个substringsubstr并转到某个索引。 I don't see how I could use a split and join either. 我看不到如何使用splitjoin Is there anyway to do this with jQuery or javascript. 无论如何,使用jQuery或javascript可以做到这一点。

my current way is very slow... 我目前的方式很慢...

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('');
            }
        }
    }

After the substrings are found I want them to be removed from my string. 找到子字符串后,我希望将其从我的字符串中删除。

After edits, you can simply do this for your requirements: 编辑后,您可以根据需要简单地执行此操作:

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,'')

With regex, you can do like this: 使用正则表达式,您可以这样:

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);
}

After this, In each iteration, the variable width will have the substring width: 4874px and the variable height will have the substring height: 898px 此后,在每次迭代中,变量width将具有子字符串width: 4874px ,变量height将具有子字符串height: 898px

An elegant way to extract string between two others is to add a function to String object. 提取其他两个字符串之间的一种优雅方法是向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> 

Assuming the format remains the same getting the index of width/height and px should be enough 假设格式保持不变,则获取宽度/高度和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.

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