0

I am looking for some help with Regex syntax. I tried many ways to make the syntax for this case, but nothing seems to work. Could someone help me with this?

So I have a case where I am trying to replace everything before the "." symbol in this string,

example_ so.myname

I successfully removed all the strings before the "." using this expression:

var removeStrBeforeDot = params.value.replace(/(\<b\>|\<nobr\>)[a-z]*\./g, "");
    removeStrBeforeDot = removeStrBeforeDot.replace(/\(.*\)\<\/b\>/g, " ");

But the issue with this expression is, this works when there is only letters before the dot and doesn't work when there are symbols like , space, etc. Like for example, example so.myname has a space and a "_" which is not working in this case. What I like to do is keep it more generic and remove any string before the "." to be removed. Any help could be greatly appreciable.

Thanks!

luxy
  • 65
  • 1
  • 4

4 Answers4

0

One solution :

var str = 'example_ so.myname';
var result = str.replace(/[^.]*.(.*)/g , '$1');

console.log(result);
document.getElementById('el').innerHTML = result;
<div id='el'></div>

Another solution : most rapid, and less expensive !

    var str = 'example_ so.myname';
    var result = str.slice(str.indexOf('.')+1)
    console.log(result);
    document.getElementById('el').innerHTML = result;
<div id='el'></div>
Anonymous0day
  • 3,012
  • 1
  • 14
  • 16
  • I tried your regex but its keeping the dots and removing all the strings. So the result I see when I run is only dots and no strings. I was not able to open the run code snippet too. – luxy Oct 13 '15 at 00:40
  • I've updated, just check it. it was missing '$1'. and i added a div in my snippet to see the result without the console – Anonymous0day Oct 13 '15 at 00:49
0

This should work. It gets the position of the first dot and then collects the characters in the string from that point forward.

var dot = str.indexOf('.');
var afterDot = str.substr(dot+1);

Don't use a regex if you can do it with a simpler approach.

user2182349
  • 9,569
  • 3
  • 29
  • 41
0

You can do like this:

st = 'example_ so.myname';
re = str.replace(/^.*\.+/g , '');
console.log(re)
Cyril Cherian
  • 32,177
  • 7
  • 46
  • 55
  • Either use `\.` or `[.]`. You don't need to escape dot inside brackets, and you don't need to put a single character in brackets. – Barmar Oct 13 '15 at 00:38
  • yea correct except that you will need to to this _\. escaping the dot_ because otherwise its a wild character for anything.. – Cyril Cherian Oct 13 '15 at 00:42
  • 1
    That's what I said, either escape it or put it in brackets, but not both. – Barmar Oct 13 '15 at 00:44
  • You don't need the `+`, since `.*` will perform a greedy match. So if there's a sequence of `.` characters in the string, the wildcard will match everything before the last one. – Barmar Oct 13 '15 at 00:45
  • Depending on how you read the OPs question, it sounds like he wants to remove characters before the dot, but not the dot itself. –  Oct 13 '15 at 03:34
0

I guess split is probably the easiest way:

   alert("example_ so.myname".split(/\./)[1]);
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • What is there's more than one dot? Why `/\./` instead of just `'.'`? –  Oct 13 '15 at 03:35