Skip to content

Commit

Permalink
Fix:Crash when matching with author names ending in ??? by escaping r…
Browse files Browse the repository at this point in the history
…egex strings #2265
  • Loading branch information
advplyr committed Oct 30, 2023
1 parent 2ef11e5 commit 9616d99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions server/finders/BookFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Audnexus = require('../providers/Audnexus')
const FantLab = require('../providers/FantLab')
const AudiobookCovers = require('../providers/AudiobookCovers')
const Logger = require('../Logger')
const { levenshteinDistance } = require('../utils/index')
const { levenshteinDistance, escapeRegExp } = require('../utils/index')

class BookFinder {
constructor() {
Expand Down Expand Up @@ -201,7 +201,7 @@ class BookFinder {
add(title, position = 0) {
// if title contains the author, remove it
if (this.cleanAuthor) {
const authorRe = new RegExp(`(^| | by |)${this.cleanAuthor}(?= |$)`, "g")
const authorRe = new RegExp(`(^| | by |)${escapeRegExp(this.cleanAuthor)}(?= |$)`, "g")
title = this.bookFinder.cleanAuthorForCompares(title).replace(authorRe, '').trim()
}

Expand Down
12 changes: 12 additions & 0 deletions server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,16 @@ module.exports.asciiOnlyToLowerCase = (str) => {
}
}
return temp
}

/**
* Escape string used in RegExp
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
*
* @param {string} str
* @returns {string}
*/
module.exports.escapeRegExp = (str) => {
if (typeof str !== 'string') return ''
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}

0 comments on commit 9616d99

Please sign in to comment.