Javascript 识别"&引用;以及在字符串中缺少空格到;如果。。。如果;
我想确定字符串中“.”之后是否没有空格 我尝试了一个嵌套的if语句,但它不起作用。我想我错过了一些非常简单的事情 此外,我读到Regex可能会这样做,但我无法理解语法Javascript 识别"&引用;以及在字符串中缺少空格到;如果。。。如果;,javascript,if-statement,indexof,Javascript,If Statement,Indexof,我想确定字符串中“.”之后是否没有空格 我尝试了一个嵌套的if语句,但它不起作用。我想我错过了一些非常简单的事情 此外,我读到Regex可能会这样做,但我无法理解语法 (function() { 'use strict'; var invocationInitial = document.getElementById('spokenNames'); if(invocationInitial) { var invocation = invocationInitia
(function() {
'use strict';
var invocationInitial = document.getElementById('spokenNames');
if(invocationInitial) {
var invocation = invocationInitial.innerHTML.trim();
}
var counter = 1;
var message = '';
if(invocation.indexOf('.') !== -1) {
if(/\s/.test(invocationInitial) === false)
{
message = counter + ". No dot in string without subsequent whitespace";
counter = counter +1;
}
}
if(message) {
alert(message);
}
})();
如果“invocationInitial”没有每个出现的点(“.”)后跟空格,则应显示浏览器警告(“消息”)
这里引入了var计数器,因为在完整版本中,将根据不同的条件显示多个浏览器警告。这里需要的正则表达式非常简单:
/\.\S/
。也就是说“匹配一个不后跟空格字符的点”。请注意,\s
表示“匹配空白字符”,而\s
(大写s)表示“匹配任何非空白字符”
因此,您可以简单地执行以下操作:
if (/\.\S/.test(invocation)) {
// There's a dot followed by non-whitespace!
}
else {
// There is no dot followed by non-whitespace.
}
完成@Brock Adams,抱歉耽搁了。我们需要学习诀窍。