Javascript 如何确定字符串是否为代码方法?

Javascript 如何确定字符串是否为代码方法?,javascript,regex,Javascript,Regex,我通过两个例子来解释我的问题: 示例1: var str1 = "this is a test this is a test"; var str2 = " this is a test this is a tes"; 我想要这个:这不是编码方法 示例2: var str1 = "this is a test this is a test"; var str2 = " this is

我通过两个例子来解释我的问题:

示例1:

var str1 = "this is a test
                this is a test";
var str2 = "    this is a test

                this is a tes";
我想要这个:
这不是编码方法

示例2:

var str1 = "this is a test
                this is a test";
var str2 = "    this is a test

                this is a tes";
我想要这个:
这是编码方法


因此,正如您在上面的示例中所看到的,如果所有行的开头至少有4个空格(在开头),那么它就是
code-method
,否则它就是
而不是code-method
。我该怎么做


我所能做的就是数数行数:

var text = str.val();   
var lines = text.split(/\r|\r\n|\n/);
var count = lines.length;
alert(count); // it is the number of lines in the string
还选择换行前的所有空格。(我不知道它是否有用)


regex
/\n?\s+/gm
将选择开头有一个或多个空格的行。您需要检查该行是否以四个空格开头

你可以用

/^ {4}/gm

  • ^
    :行的开始
  • {4}
    :匹配四个空格
  • gm
    :全局和多行标志
  • //RegEx
    变量linebreak=/\r\n |\r |\n/g,
    编码=/^{4}/gm;
    函数isCoded(str){
    返回str.split(linebreak).length==(str.match(编码)| |[]).length;
    }
    var str1=`这是一个测试
    这是一个,
    str2=`这是测试
    未编码`;
    
    document.body.innerHTML=str1+':是否已编码?'+isCoded(str1)+'
    '+str2+':是否已编码?'+iscode(str2)正则表达式将选择开头有一个或多个空格的行。您需要检查该行是否以四个空格开头

    你可以用

    /^ {4}/gm
    

  • ^
    :行的开始
  • {4}
    :匹配四个空格
  • gm
    :全局和多行标志
  • //RegEx
    变量linebreak=/\r\n |\r |\n/g,
    编码=/^{4}/gm;
    函数isCoded(str){
    返回str.split(linebreak).length==(str.match(编码)| |[]).length;
    }
    var str1=`这是一个测试
    这是一个,
    str2=`这是测试
    未编码`;
    document.body.innerHTML=str1+':是否已编码?'+isCoded(str1)+'
    '+str2+':是否已编码?'+iscode(str2)类似

    > str1.split(/\r|\r\n|\n/).length == str1.match(/^\s{4,}/gm).length
    < false
    
    >str1.split(/\r |\r\n |\n/).length==str1.match(/^\s{4,}/gm).length
    

    >str2.split(/\r |\r\n |\n/).length==str2.match(/^\s{4,}/gm).length
    
    类似

    > str1.split(/\r|\r\n|\n/).length == str1.match(/^\s{4,}/gm).length
    < false
    
    >str1.split(/\r |\r\n |\n/).length==str1.match(/^\s{4,}/gm).length
    

    >str2.split(/\r |\r\n |\n/).length==str2.match(/^\s{4,}/gm).length
    
    如果要确定字符串是否有效,则需要返回布尔值

    您可以在换行符上拆分字符串,然后使用带有谓词函数的
    each
    方法来测试每一行是否符合您的条件

    function isCodeMethod(string) {
      const hasIndent = /^\s{4}/;
      return string
        .split("\n")
        .every(s => hasIndent.test(s));
    }
    
    有一些测试输入

    // false
    isCodeMethod("this is a test")
    
    // true
    isCodeMethod("    this is a test")
    
    // false
    isCodeMethod(`    this is a test1
    this is a test2`)
    
    // true
    isCodeMethod(`    this is a test1
    this is a test2`)
    
    // true
    isCodeMethod(`    this is a test1
                      this is a test2`)
    

    如果要确定字符串是否有效,则需要返回布尔值

    您可以在换行符上拆分字符串,然后使用带有谓词函数的
    each
    方法来测试每一行是否符合您的条件

    function isCodeMethod(string) {
      const hasIndent = /^\s{4}/;
      return string
        .split("\n")
        .every(s => hasIndent.test(s));
    }
    
    有一些测试输入

    // false
    isCodeMethod("this is a test")
    
    // true
    isCodeMethod("    this is a test")
    
    // false
    isCodeMethod(`    this is a test1
    this is a test2`)
    
    // true
    isCodeMethod(`    this is a test1
    this is a test2`)
    
    // true
    isCodeMethod(`    this is a test1
                      this is a test2`)
    

    更新了你的。现在应该可以用了。更新了你的。现在应该可以用了。谢谢,你的正则表达式可能是个线索。。!实际上,我需要确定字符串是否为
    code方法
    。。!实际上,它应该检查是否有超过4个空格,所以使用
    {4,}
    @stack,但是测试失败,因为作者使用了
    \s
    ,这也允许使用其他空格。如果一个人给出一个
    换行符
    ,然后是
    3
    空格,那么它就会失败@堆栈在
    {4}
    中不需要
    。如果字符串包含
    5
    6
    7
    。。。空格,它还包含4个空格。@stack Yes,还请注意,
    [abc]+
    将匹配一个字符串,该字符串按任意顺序包含
    a
    b
    c
    ,一次或多次。我在上一篇文章中添加了链接,这是学习正则表达式的好参考。谢谢,你的正则表达式可能是一个线索。。!实际上,我需要确定字符串是否为
    code方法
    。。!实际上,它应该检查是否有超过4个空格,所以使用
    {4,}
    @stack,但是测试失败,因为作者使用了
    \s
    ,这也允许使用其他空格。如果一个人给出一个
    换行符
    ,然后是
    3
    空格,那么它就会失败@堆栈在
    {4}
    中不需要
    。如果字符串包含
    5
    6
    7
    。。。空格,它还包含4个空格。@stack Yes,还请注意,
    [abc]+
    将匹配一个字符串,该字符串按任意顺序包含
    a
    b
    c
    ,一次或多次。我在上一篇文章中添加了链接,这是学习regex的好参考。您的代码有以下错误:Uncaught SyntaxError:Unexpected token=>。您可以修复它吗?您可以使用支持ES2015 arrow函数的浏览器,也可以查看。您的代码有以下错误:未捕获语法错误:意外标记=>。您可以修复它吗?您可以使用支持ES2015箭头功能的浏览器,也可以查看。