[英]How to remove string between specified characters and store the removed string (Javascript)
Let's say I have a string: 假设我有一个字符串:
"but i [C#min] do [G#min] believe there's"
How to I turn that string into: 如何将字符串转换为:
"but i do believe there's" (basically removing everything in-between '[' and ']')
And I would like to have [C#min]
& [G#min]
stored in an another array. 我想将[C#min]
和[G#min]
存储在另一个数组中。
var a = "but i [C#min] do [G#min] believe there's"; console.log(a.replace(/\\[(.[^\\[\\]]*)\\]/g, '')); // "but i do believe there's" console.log(a.match(/\\[(.[^\\[\\]]*)\\]/g)); // ["[C#min]", "[G#min]"]
The RegExp matches for everything between [
, ]
except for [
, ]
themselves. RegExp匹配[
, ]
之间的所有内容,除了[
, ]
本身。
You might need to remove extra white spaces left around the replacements. 您可能需要删除替换项周围剩余的空白。
You may use a regular expression: 您可以使用正则表达式:
var text = "but i [C#min] do [G#min] believe there's"; var regexp = /(\\[[^\\]]*\\])/g; var matches = text.match(regexp); text = text.replace(regexp, '').replace(/ /g, ' '); console.log(text);
matches
will contain the array ["[C#min]", "[G#min]"]
. matches
将包含数组["[C#min]", "[G#min]"]
。
Note: the second replace
for text handles the incorrectly remaining double spaces. 注意:第二个replace
为text的字符将处理错误地保留的双精度空格。
Try this straightforward way: 试试这种简单的方法:
var str = "but i [C#min] do [G#min] believe there's"; var start, stop, newStr = '', removed = '', counter =1; while(str.search(/\\[.*\\]/g) > 0) { start = str.indexOf('['); stop = str.indexOf(']'); newStr += str.substring(0, start); removed += str.substring(start, stop+1); str = str.substring(stop+1, str.length); counter++; if(counter > 3) break; } newStr += str; console.log(newStr);
Without Regex, simple way: 没有正则表达式,简单的方法是:
var string = "but i [C#min] do [G#min] believe there's"; array = []; filteredString = "" for (i=0;i<string.length;i++){ if (string[i] != '[') { filteredString += string[i]; continue; } newstring = ""+string[i]; do { newstring += string[++i]; } while (string[i]!=']'); array.push(newstring); } console.log(array); console.log(filteredString);
You can use a regular expression to find the bracketed strings and then strip them out with String.replace()
. 您可以使用正则表达式查找括号中的字符串,然后使用String.replace()
它们。 The stripped out portions will be in an array for whatever use you need them for. 剥离的部分将按阵列排列,以供您使用。
var input = "but i [C#min] do [G#min] believe there's"; var reg = /\\[(.*?)\\]/g; // Patthen to find var matches = input.match(reg); // Find matches and populate array with them var output = input.replace(reg, "").replace(/\\s\\s/g, " "); // Strip out the matches and fix spacing console.log(matches); // Just for testing console.log(output); // Result
You could split the string by [: 您可以将字符串分割为[:
function parseString(input) {
output = {
values: [],
result: ""
};
input = input.split('[');
for (var index = 0; index < input.length; index++) {
var closingPosition = input[index].indexOf("]");
if ((index > 0) && (closingPosition + 1)) {
output.values.push('[' + input[index].substring(0, closingPosition) + ']');
if (input[index].length > closingPosition) {
output.result += input[index].substring(closingPosition + 1);
}
} else {
output.result += input[index];
}
}
return output;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.