1

I have a situation where I have 3 font files and I read its content in order to find mathes with font name. But the thing is that font names are Wingdings, Wingdings 2, Wingdings 3. And when I have Wingdings font name it matches all 3 files, but I need file that exactly is associated with font name, not all 3 of them. I tried to find it using indexOf method, but it didn't help. The only rational way is to use regular expression, but cannot think of a right one. One more thing need to be mentioned is that I have to pass a parameter into that regExp, something like

var regExp = new RegExp('\\^' + fontName + '$\\', 'g');
if (currentFileContent.search(regExp) !== -1) {...}

Any help will be greatly appreciated.

2
  • 1
    Try var regExp = RegExp('^' + fontName + '$'); if (regExp.test(currentFileContent)) { ... }. Commented Sep 30, 2015 at 18:46
  • Also, you may need to escape the font name. Commented Sep 30, 2015 at 18:52

1 Answer 1

4

It seems you try to use regex delimiters in a RegExp constructor. You only need /.../ in the literal notation.

Note you need not escape the start and end of string anchors, they lose their special meaning in the regex then. \\ matches a single \, but it cannot be matched after end of string ($).

Also, you can use RegExp#test() function to check if the string matches the pattern (note no g modifier can be used with it):

var regExp = RegExp('^' + fontName + '$'); 
if (regExp.test(currentFileContent)) { ... }

If font names contain special characters, use escapeRegExp function from MDN:

function escapeRegExp(string){
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}

And then

var regExp = RegExp('^' + escapeRegExp(fontName) + '$'); 

And the final note: if the font names appear inside a larger string, and you need to match Windings but not Windings3, use

var regExp = RegExp('\\b' + escapeRegExp(fontName) + '\\b'); 

The \b is a word boundary.

UPDATE

To make sure you only match a font name that is not followed by a whitespace (if any) and a digit, use a (?!\\s*\\d) lookahead when declaring a RegExp:

var fontName = "Wingding";
var contents = "Font name: Wingding, the other file: Font name: Wingding 2. And so forth. ";
var rExp = RegExp(fontName + '(?!\\s*\\d)');
if (rExp.test(contents)) {
   document.write(fontName + " was found in '<i>" + contents + "</i>'.");
}

Sign up to request clarification or add additional context in comments.

9 Comments

Also, g makes no sense when you want to match a whole string with your regex.
a tried, but it mathes all 3 files again. here may be the case where there's a space between Windings 3, not Windings3. and font name is just Windings. maybe check if there's characters after space?
Then just check for Windings, like /Windings(?!\s*\d)/.test(file).
Sorry, but how can I pass a variable fontName instead of Windings string here?
Using RegExp: var regExp = RegExp(fontName + '(?!\\s*\\d)');
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.