Skip to content

Instantly share code, notes, and snippets.

@nbogie
Last active June 21, 2016 01:56
Show Gist options
  • Select an option

  • Save nbogie/c3f7a8e73190778a611e520b7c30e35b to your computer and use it in GitHub Desktop.

Select an option

Save nbogie/c3f7a8e73190778a611e520b7c30e35b to your computer and use it in GitHub Desktop.
trim as regex in js
function trim(str){
return str.replace(/^\s*((?:.|\n)*?)\s*$/, "$1");
}
var testCases = [
[" a" , "a"],
["A " , "A"],
[" a " , "a"],
["a b! " , "a b!"],
[" .- " , ".-"],
["\nfoo" , "foo"],
["\t\t\n You \njust \t ", "You \njust"],
];
var testReport = testCases.map(function(test) {
var [input, expected] = test;
var output = trim(input);
var successStr = output === expected ? "PASS" : "FAIL";
return {
result: successStr,
input: input,
expected: expected,
was: output
};
});
testReport.filter(r => r.result === 'FAIL')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment