A
A
Alexander Sharomet2016-08-06 14:15:55
JavaScript
Alexander Sharomet, 2016-08-06 14:15:55

How to get text after dot?

Hello.
Tell me how you can get the string after the dot using regular expressions.
Example
From this line: Hello .main World
I want to do something like this: Hello class="main" World

var str = "Hello .main World";
re = /\.(.*)/g;
found = str.match(re);
a=found.replace(/\./g, "class=\"\""); // Uncaught ReferenceError: replace is not function
console.log(a);

Thank you.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Deodatuss, 2016-08-06
@sharomet

/\.[az-_1-9]+\b/gim
UPD:

var str = "Hello ......main World. Hop hop .test .test wefwef .test";
var regex = /(\.[a-z-_1-9]+)\b/gim;
var match;
var matches = [];
do {
  match = regex.exec(str);
  if (match && match[1]) {
    matches.push(match[1])
  }
} while (match);
matches.forEach(function(match){
  var regex = new RegExp(match, 'g');
  var className = match.replace(".","");
  var classString = "class='"+ className +"'";
  str = str.replace(regex, classString)
})
alert(str);

V
Vladimir Grabko, 2016-08-06
@VGrabko

A couple of Split will relieve suffering

A
Alexander Sharomet, 2016-08-06
@sharomet

Everything is simpler
var str = "Hello .main World .biba .sharomet";
console.log(str.replace(/\.(\w+)/g, 'class="$1"'));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question