-3

How do I replace anything that goes after certain character? For example:

var temp = 'abcdefghikj';

and I want to replace all characters with nothing after c:

temp.replace('c*', '');

I looked at this topic but none of the suggested solutions worked for me: Regular Expressions- Match Anything

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
Mark
  • 161
  • 11
  • `temp = temp.substring(0, temp.indexOf("c") + 1);` – ASDFGerte Sep 29 '19 at 22:12
  • I don't know what I'm doing wrong but the expression `\?(.*)` isn't working for me. In my case I need to edit it to `\c(.*)` or perhaps to `\c.*` but it isn't working when used in `temp.replace('\c.*', '');` – Mark Sep 29 '19 at 22:18

1 Answers1

0

What you wrote is right. You're there:

var temp = 'abcdefghikj';
temp = temp.replace(/c(.*)$/ig, "c");
console.log(temp);

Make sure you also need save the return to the original value.

You don't need to use RegEx for this simple one. You can simply find the first index of c and slice it:

var temp = 'abcdefghikj';
temp = temp.substr(0, temp.indexOf("c") + 1);
console.log(temp);
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252