我有一个以下格式的字符串,
Hello this #result1# is a sample #result2# string with a lot of # and #result3# i need to find all the values between #
我需要基于javascript或nodejs的解决方案,只能返回['result1' , 'result2' , 'result3']
。
规则:
#
。['result1# is a sample #result2# string with a lot of ' , 'result1# is a sample ']
答案 0 :(得分:1)
var str = 'Hello this #result1# is a sample #result2# string with a lot of # and #result3# i need to find all the values between #';
console.log(str.match(/#[^#\s]+#/g));

<强>解释强>
/ : regex delimiter
# : # character
[^#\s]+ : 1 or more character that is NOT # or space
# : # character
/g : regex delimiter and global flag
答案 1 :(得分:0)
以下代码段应该有效:
let str = "Hello this #result1# is a sample #result2# string with a lot of # and #result3# i need to find all the values between #";
let identifier = '#';
function getSubstrings(str, identifier) {
let results = [];
let splittedItems = str.split(identifier);
splittedItems.forEach(
function(item) {
if (item.length > 1 && item.indexOf(' ') === -1) {
results.push(item);
}
}
);
return results;
}
document.write(getSubstrings(str, identifier));
&#13;
答案 2 :(得分:0)
/#(.*?)#
在行动here
中查看答案 3 :(得分:0)
您的问题可能有许多特殊情况,如果没有关于您尝试过的内容以及失败的详细信息,很难给出明确的答案。 您可以尝试从以下开始:
var input = 'Hello this #result1# is a sample #result2# string with a lot of # and #result3# i need to find all the values between #';
output = input.match(/#[A-Za-z0-9]+#/g);
console.log(output); //["#result1#", "#result2#", "#result3#"]
[A-Za-z0-9]
匹配任何大写字母(A-Z
),小写字母(a-z
)和数字(0-9
)
+
表示前面的模式至少应出现一次
g
表示全局,因此匹配字符串
它应该让你去。