1

I have this Regex below that I use together with Textcomplete, check if someone writes # or @ together with a parameter

Regex:

/\B#(\w+)$/

I load some names from my database, and you can search them with either # or @. Lets say that in the database it contains Stackoverflow.

If I write #Stackoverflow it works since I wrote a big S letter. but if I write #stackoverflow it dont work because of the small s letter.

My question is how can I change the regex so it ignore small or big letters?

Updated with script

$('textarea').textcomplete([
               { // html
                   mentions: Companies,
                   match: /\B#(\w+)\i$/,
                   search: function (term, callback) {
                       callback($.map(this.mentions, function (mention) {
                           return mention.indexOf(term) === 0 ? mention : null;
                       }));
                   },
                   template: function (value) {
                       return '<img src="/emoji/' + value + '.png"></img>' + value;
                   },
                   index: 1,
                   replace: function (mention) {
                       return '#' + mention + ' ';
                   }
               }
            ],
            { appendTo: 'body' }).overlay([
                   {
                       match: /\B#\w+/g,
                       css: {
                           'background-color': '#d8dfea',
                       }
                   }
            ])

To show you that It dont work, I created it at codePen.

Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77

2 Answers2

2

You can use the /i option to ignore the case. So your regex will be

/\B#(\w+)$/i

EDIT:

Just checked your regex and it works fine without the /i

DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

I modified the callback at the end as

return mention.toLowerCase().indexOf(term.toLowerCase()) === 0 ? mention : null;

Here is an updated codepen.

Now, it works:

enter image description here

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563