I have been trying to find a way to recognize DOI on an input form to trigger a specific search. I found a DOI regular expression here and when I use it with Match or Test I got 'NULL' and Error as result
function checkDOI(string){
//Redundant. I know
var testKey = String(string);
var DOIpattern = '\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b';
var found = DOIpattern.test(testKey);
console.log("found", found + " DOI "+ testKey);
return found
}
checkDOI("10.1016.12.31/nature.S0735-1097(98)2000/12/31/34:7-7")
I got this error DOIpattern.test is not a function
Then if I change the found.test for MATCH var found = DOIpattern.match(testKey);
The result is NULL
Does anybody can tell me what I am doing wrong?
Thank in advance!
test
is a method of RegExp
class and not of String
. To correct, create a RegExp object using the constructor and call the test
method on it.
function checkDOI(string) {
//Redundant. I know
var testKey = String(string);
var DOIpattern = '\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?!["&\'<>])\S)+)\b';
var found = new RegExp(DOIpattern).test(testKey);
console.log("found", found + " DOI " + testKey);
return found
}
checkDOI("10.1016.12.31/nature.S0735-1097(98)2000/12/31/34:7-7")
As long as your RegExp string is correct, the input should match.