Programming Language Identification in JS This is a RegExp exercice from HackerRank. Basically, I have to write a program (I chose JavaScript) that receives a program as an input (string) and tries to identify the programming language used. We're only concerned by 3 languages: C, Java or Python.

function processData(input) {
   console.log(identifyLanguage(input));
} 

var identifyLanguage = function (code) {    
    if (isJava(code)) 
        return 'Java';
    else if (isC(code))
        return 'C';
    else if (isPython(code))
        return 'Python';
    else return 'C';
}

var isC = function(input) {    
    const cPreprocessor = /#(include.*?&gt;|define.*)/g;
    const cStruct = /\b(typedef.*?;|struct.*?(?=\s*?\{))/g;
    const cPointers = /\b\w+\s*-&gt;\s*\w+\b/g;
    const cFunctionPointerReturn = /[a-zA-Z]+\s+\*\s+([a-zA-Z_]\w*)\s*\([a-zA-Z]*\s*([a-zA-Z_]\w*)\)\s*\{/g;
    const cFunctionPointerArg = /[a-zA-Z]+\s*\*?\s*([a-zA-Z_]\w*)\(.*[a-zA-Z]+\s*\*\s*([a-zA-Z_]\w*).*\)\s*\{/g;
    return cPreprocessor.test(input) || cStruct.test(input) || cPointers.test(input) ||
           cFunctionPointerArg.test(input) || cFunctionPointerReturn.test(input);
}

var isPython = function (input) {
    const pythonKeywords = /(class|def|if|while|else|for).*:/g;
    const pythonLists = /\[((\d+|("|').+\3),)*(\d+|("|').+\5)\]/g;
    const pythonPrint = /\bprint(\s*("|').+\2|\(.*\))/g;
    const pythonBoolean = /True|False/g;
    return pythonKeywords.test(input) || pythonLists.test(input) || 
           pythonPrint.test(input) || pythonBoolean.test(input);
}

var isJava = function(input) {
    const javaImport = /\bimport\s*.*?;/g;
    const javaClass = /(public|private)?\s*class.*?\{/g;
    return javaImport.test(input) || javaClass.test(input);
}


I can't say I'm satisfied with this, although it passes all the test cases, but It feels kinda ugly, non-DRY and amateurish. What can I do to improve it?

Update

The previous code updated after the suggestions provided in the chosen answer by Joseph :

var langPatterns = {
    C: [
        /#(include.*?&gt;|define.*)/g, //cPreprocessor
        /\b(typedef.*?;|struct.*?(?=\s*?\{))/g, //cStruct
        /\b\w+\s*-&gt;\s*\w+\b/g, //cPointers
        /[a-zA-Z]+\s+\*\s+([a-zA-Z_]\w*)\s*\([a-zA-Z]*\s*([a-zA-Z_]\w*)\)\s*\{/g, //cFunctionPointerReturn
        /[a-zA-Z]+\s*\*?\s*([a-zA-Z_]\w*)\(.*[a-zA-Z]+\s*\*\s*([a-zA-Z_]\w*).*\)\s*\{/g //cFunctionPointerArg
    ],
    Java: [
        /\bimport\s*.*?;/g, //javaImport
        /(public|private)?\s*class.*?\{/g //javaClass
    ],
    Python: [
        /(class|def|if|while|else|for).*:/g, //pythonKeywords
        /\[((\d+|("|').+\3),)*(\d+|("|').+\5)\]/g, //pythonLists
        /\bprint(\s*("|').+\2|\(.*\))/g, //pythonPrint
        ///True|False/g //pythonBoolean
    ],
}

var identifyLanguage2 = function (code) {
    return Object.keys(langPatterns).find(lang =&gt; 
        langPatterns[lang].some(pattern =&gt; 
            pattern.test(code)));
}
Instead of ||, you could stuff the patterns in an array and use array.some(). If one of the patterns match the string, it will return immediately with true and skipping the rest of the array.

const patterns = []
patterns.push(pattern1)
patterns.push(pattern2)
patterns.push(pattern3)
return patterns.some(p =&gt; string.match(p))


Taking it further, you can use a key-value pair of language name and patterns. Then use Object.keys and array.find to find which key has the set of patterns that match the string. For the sake of brevity, variable names are shortened.

const l = {
  java: [/* array of java patterns */],
  python: [/* array of python patterns */],
  c: [/* array of c patterns */]
}

// "For each language in languages, find the language that contains a
// pattern that matches the string"
const language = Object.keys(l).find(k =&gt; l[k].some(p =&gt; s.match(p)))


Also note that some languages have common syntaxes. If you want to go this route, you'll have to find syntax that's unique to the language or risk mistaking one language for another. For instance, True may be a Python boolean... or a variable name in Java. The same with class, is it a Python class or Java class. What if I defined a string in Java that's valid Python?