Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
/\.[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);
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 questionAsk a Question
731 491 924 answers to any question