多行字符串 findIndex()
multiline string findIndex()
我正在尝试在我的字符串中查找多行子字符串。
我的代码:
data = `if (res) {
new PNotify({
title: TAPi18n.__('success'),
text: TAPi18n.__('image_uploaded'),
type: 'info',
styling: 'bootstrap3'
});
}
if (err) {
return new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
});
}`;
/*not found...*/
match = `new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
})`;
console.log(data);
console.log(match);
console.log(data.indexOf(match));
那行 console.log(data.indexOf(match));
显示了我 -1
。
我的代码有什么问题?我该如何进行多行搜索?
问题是 match
和 data
之间的空格数量不匹配。
您可以将 match
转换为允许可变数量空格的正则表达式。
第一个 match.replace()
用于转义字符串中的所有特殊正则表达式字符,第二个将空格转换为 \s+
因此它将匹配任何数量。
data = `if (res) {
new PNotify({
title: TAPi18n.__('success'),
text: TAPi18n.__('image_uploaded'),
type: 'info',
styling: 'bootstrap3'
});
}
if (err) {
return new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
});
}`;
match = `new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
})`;
re = new RegExp(match.replace(/[-\/\^$*+?.()|[\]{}]/g, '\$&').replace(/\s+/g, '\s+'));
result = data.match(re);
console.log(result && result.index);
您需要规范化您的字符串,然后再比较它们
这是一个简单的函数,可以按行拆分字符串并在将所有内容重新组合在一起之前删除每行的空格
function normalizeString(str){
return str.split('\n').map(e => e.trim()).join('')
}
输出:
data = `if (res) {
new PNotify({
title: TAPi18n.__('success'),
text: TAPi18n.__('image_uploaded'),
type: 'info',
styling: 'bootstrap3'
});
}
if (err) {
return new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
});
}`;
/*not found...*/
match = `new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
})`;
// normalize strings
var normalizedData = data.split('\n').map(e => e.trim()).join('')
var normalizedMatch = match.split('\n').map(e => e.trim()).join('')
console.log(normalizedData)
console.log(normalizedMatch)
console.log(normalizedData.indexOf(normalizedMatch));
我正在尝试在我的字符串中查找多行子字符串。
我的代码:
data = `if (res) {
new PNotify({
title: TAPi18n.__('success'),
text: TAPi18n.__('image_uploaded'),
type: 'info',
styling: 'bootstrap3'
});
}
if (err) {
return new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
});
}`;
/*not found...*/
match = `new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
})`;
console.log(data);
console.log(match);
console.log(data.indexOf(match));
那行 console.log(data.indexOf(match));
显示了我 -1
。
我的代码有什么问题?我该如何进行多行搜索?
问题是 match
和 data
之间的空格数量不匹配。
您可以将 match
转换为允许可变数量空格的正则表达式。
第一个 match.replace()
用于转义字符串中的所有特殊正则表达式字符,第二个将空格转换为 \s+
因此它将匹配任何数量。
data = `if (res) {
new PNotify({
title: TAPi18n.__('success'),
text: TAPi18n.__('image_uploaded'),
type: 'info',
styling: 'bootstrap3'
});
}
if (err) {
return new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
});
}`;
match = `new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
})`;
re = new RegExp(match.replace(/[-\/\^$*+?.()|[\]{}]/g, '\$&').replace(/\s+/g, '\s+'));
result = data.match(re);
console.log(result && result.index);
您需要规范化您的字符串,然后再比较它们
这是一个简单的函数,可以按行拆分字符串并在将所有内容重新组合在一起之前删除每行的空格
function normalizeString(str){
return str.split('\n').map(e => e.trim()).join('')
}
输出:
data = `if (res) {
new PNotify({
title: TAPi18n.__('success'),
text: TAPi18n.__('image_uploaded'),
type: 'info',
styling: 'bootstrap3'
});
}
if (err) {
return new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
});
}`;
/*not found...*/
match = `new PNotify({
title: TAPi18n.__('error'),
text: err,
type: 'error',
styling: 'bootstrap3'
})`;
// normalize strings
var normalizedData = data.split('\n').map(e => e.trim()).join('')
var normalizedMatch = match.split('\n').map(e => e.trim()).join('')
console.log(normalizedData)
console.log(normalizedMatch)
console.log(normalizedData.indexOf(normalizedMatch));