你能帮我写一个纯正则表达式来找到一个不在字符串中重复的第一个字母吗?我想我可能需要使用负向前瞻和负面看后方,但我认为javascript不支持lookbehind。
例如
'NUNUMUNN' // expected output 'M'
'LOMING' // expected output 'L'
我认为使用常规字符串操作可以做到这一点,但我的偏好是真正的纯正则表达式。
我的出发点目前是:
/(a-zA-Z).*?(?!\1)/.match('thestring');
但它不起作用。
答案 0 :(得分:3)
改变你的逻辑:首先匹配一个单词中重复的所有字母,然后匹配下一个字母 - 那就是你需要看的字母。然后有一些边缘情况需要考虑。
\b # Start of word
(?: # Start of non-capturing group (optional, see below)
(?: # Start of non-capturing group that matches...
([a-z]) # ...and captures any ASCII letter
(?= # if it's followed by
[a-z]* # zero or more letters
\1 # and the same letter again.
) # (end of lookahead assertion)
)+ # Repeat at least once
(?!\1) # Assert that this letter doesn't follow immediately to avoid matching "AAA"
)? # Make that group optional (in case there are no repeated letters in the word)
([a-z]) # Then match a letter and capture it in group 2.
(?![a-z]\2) # and make sure that letter doesn't immediately repeat either.
<强>解释强>
mysqlConnection.query('SELECT `value` FROM `info` WHERE `name`=\'maxitems\'', function(err, row, fields) {
var maxitems = 10;
if(err || row.length == 0) {
console.log('Mysql error or empty result:', err);
}
else {
maxitems = row[0].value;
}
if(offer.items_to_receive.length > maxitems) {
.....
}
});
请注意,您需要查看匹配的第2组才能获得结果 - 第1组将包含第一个非重复字母之前的内容。