I want to use regular expression for error message...
try {
throw new Error("Foo 'bar'");
} catch (err) {
console.log(getInQuotes(err));
}
... where getInQuotes is a function for string:
var getInQuotes = function(str) {
var re;
re = /'([^']+)'/g;
return str.match(re);
};
... but got error:
Object Error: Foo 'bar' has no method 'match'
Although it works for usual string:
console.log(getInQuotes("Hello 'world'"));
result:
[ '\'world\'' ]
Tried to stringify Error object ...
console.log("stringify: " + JSON.stringify(err));
... but it's empty:
stringify: {}
I want to use regular expression for error message...
try {
throw new Error("Foo 'bar'");
} catch (err) {
console.log(getInQuotes(err));
}
... where getInQuotes is a function for string:
var getInQuotes = function(str) {
var re;
re = /'([^']+)'/g;
return str.match(re);
};
... but got error:
Object Error: Foo 'bar' has no method 'match'
Although it works for usual string:
console.log(getInQuotes("Hello 'world'"));
result:
[ '\'world\'' ]
Tried to stringify Error object ...
console.log("stringify: " + JSON.stringify(err));
... but it's empty:
stringify: {}
Share
Improve this question
asked Jun 28, 2013 at 11:45
Maxim YefremovMaxim Yefremov
14.2k28 gold badges123 silver badges170 bronze badges
4 Answers
Reset to default 5You created an Error object, and that's not a string. But you can simply solve this by calling its toString
method, and applying match on the result of that:
function getInQuotes(err) {
var re;
re = /'([^']+)'/g;
return err.toString().match(re);
};
Just try this http://jsfiddle/B6gMS/1/
getInQuotes(err.message)
Basically, here we are trying to get the annotated string. if we go with Regular Expression then we need a right case if there is a real need for this.
if not, below string replace would be easier solution.
// Arrow Function read given string and strip quoted values.
// Basic example
const getInQuotes = (str) => str.replace( /^[^']*'|'.*/g, '' );
In order to keep it more generic. below function helps to keep this annotator (') can be configurable. below code is in latest ES2021 code.
- This uses template literals in RegExp Function.
- Annotator is configurable
- change the method name
getInQuotes
togetAnnotatedStrings
which is meaningful. - method return always array in order to keep it predictable and avoid other errors.
- Its good not to pass entire object since its requires only the error message.
function getAnnotatedStrings(errorMessage, annotator = "'") {
if (!annotator || !errorMessage) return []
const regex = new RegExp(`${annotator}([^']+)${annotator}`, 'g')
return errorMessage.toString().match(regex);
}
Actual in ES2021 Code.
try {
throw new Error("Foo 'bar'");
} catch (e) {
console.log(getAnnotatedStrings(e?.message)); // outputs - bar
}
Refer:
Error Object Intro
Regex Functions and Basics
Template Literals
err
is not a string it's an Error
object, so it has no .match()
function . You Should call the function using Error
object's toString()
method, just this way:
try {
throw new Error("Foo 'bar'");
}
catch (err) {
console.log(getInQuotes(err.toString()));
}