1

I am implementing a search input and this is my code:

 "Author: Tiara Jordan Date: 02/23/17 Title: The Big Bang Theory",.replace(new RegExp( 't', 'g' ),`<strong>t</strong>`)

This would result to:

"Au<strong>t</strong>hor: <strong>T</strong>iara Jordan Da<strong>t</strong>e: 02/23/17 <strong>T</strong>i<strong>t</strong>le: <strong>T</strong>he Big Bang <strong>T</strong>heory"

How do i skip the Author, Title and Date matching the regexp? my expexted result would be like this:

"Author: Tiara Jordan Date: 02/23/17 Title: The Big Bang Theory"

gpbaculio
  • 5,693
  • 13
  • 60
  • 102
  • 3
    what is your expected result? – V. Sambor Sep 23 '18 at 19:14
  • whats ur expected result ? – Ewomazino Ukah Sep 23 '18 at 19:19
  • actually it can include more text, like this, "Author: Tiara Jordan Date: 02/23/17 Title: The Big Bang Theory", I want the Author, Title, Date, not matched – gpbaculio Sep 23 '18 at 19:20
  • @iamgpbaculio Your current `new RegExp( 't', 'g' )` regex can't match `T`, are you actually using `new RegExp( 't', 'gi' )`? And do you have a full list of words (or, better, all *contexts* that can be described with the regex "regular language") you want to avoid matching? – Wiktor Stribiżew Sep 23 '18 at 19:20
  • Could you be more precise in your question and put a real example of what do you expect to see... ? Do you want to bold all the T's in the author section ? is that right? – V. Sambor Sep 23 '18 at 19:22
  • 2
    @V.Sambor sorry for the bad question, I have edited the question – gpbaculio Sep 23 '18 at 19:23
  • I think you need `.replace(/(Date|Author|Title)|t/gi, (x,y) => y ? y : \`${x}\`)`. Probably, a safer regex would be the one that also requires `:` after these words you need to avoid matching, `/(Date|Author|Title):|t/gi`. – Wiktor Stribiżew Sep 23 '18 at 19:30
  • @WiktorStribiżew actually the 't' here is a variable so I used new RegExp, .replace(new RegExp( t, 'g' ), `${t}`) sorry i just have difficulty with this, how do i add: /(Date|Author|Title)|t/gi with a variable? – gpbaculio Sep 23 '18 at 19:34

1 Answers1

1

You need a regex that will have two main alternative branches: one will match the context you want to avoid modifying, and the other will match your variable text. Capture one of them to be able to apply different replacement logic later:

.replace(
     new RegExp("\\b(Date|Author|Title)\\b|" + t.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "gi"), 
     (x,y) => y ? y : `<strong>${x}</strong>`)

Here, \\b(Date|Author|Title)\\b| will match and capture one of the whole words Date, Author or Title, and the t.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&') (this replace is escaping regex method) will match the literal text held in t variable.

The replacement will be the following: if Group 1 matched, y will hold some text, and only this text will be used to replace the match. Else, the match will be wrapped with <strong> tags.

JS demo:

var s = "Author: Tiara Jordan Date: 02/23/17 Title: The Big Bang Theory";
var t = "t";
console.log(s.replace(
     new RegExp("\\b(Date|Author|Title)\\b|" + t.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'), "gi"), 
     (x,y) => y ? y : `<strong>${x}</strong>`)
)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563