我实际上是JavaScript的新手。我目前正在做字符串匹配的工作。但是,我可以理解为什么它是可用的,等等,但是,每当我试图使它具有动态性时,由于某种原因,我的输出是'null‘。
function matchString(str, match) {
let result = str.match('/' + match + '/g');
console.log('Output: ' + result);
}matchString('Hello溢出‘,'over');// null
function matchString(str, match, para) {
let result = str.match('/' + match + '/' + para);
console.log('Output: ' + result);
}matchString('Hello溢出‘,'over','g');// null
我要它在我的控制台输出“Over”
发布于 2019-08-21 08:30:13
有几件事你必须改变:
RegExpover Vs Over是另一回事。使用i使其不区分大小写以下是您修改的代码:
// With RegExp, case insensitive
function matchString1(str, match) {
let result = str.match(new RegExp(match, 'ig'));
console.log('Output: ' + result);
}
// With RegExp, case insensitive
function matchString2(str, match, para) {
let result = str.match(new RegExp(match, para));
console.log('Output: ' + result);
}
// Without RegExp, case sensitive
function matchString3(str, match) {
let result = str.match(match);
console.log('Output: ' + result);
}
matchString1('Hello Stack Overflow', 'over');
matchString2('Hello Stack Overflow', 'over', 'ig');
matchString3('Hello Stack Overflow', 'Over');
https://stackoverflow.com/questions/57587412
复制相似问题