var test = "Hello all, this is a message and i want to mention #john, #smith and #jane...";
And what i want to get is:
var result = ["john", "smith", "jane"];
i can take the last username in the string but not all of them. I am OK with regexp or other string functions.
Thank you.
It seems that it's not possible using a single regex :
var result = test.match(/#\w+/g).join('').match(/\w+/g);
You might need to deal with situations in which the regex finds nothing :
var result = test.match(/#\w+/g);
result = result ? result.join('').match(/\w+/g) : [];
var test = "Hello all, this is a message and i want to mention #john, #smith and #jane...";
var patt = /(^|\s)#([^ ]*)/g;
var answer = test.match(patt)
Should get what you want
Like this JSfiddle
Try this regex
/(^|\W)#\w+/g
JavaScript:
var test = "Hello all, this is a message and i want to mention #john, #smith and #jane";
var names = test.match(/(^|\W)#\w+/g);
console.log(names);
Result:
0: "#john"
1: "#smith"
2: "#jane"
Live example on RegExr:
http://regexr.com?36t6g
Related
i'm trying to get the first word after a determined word, that's how i'm doing:
Text:
Companie: 'Stack over flow';
Regex:
var reg = new RegExp('Companie' + '.*?(\\w\\S*)', 'i');
var match = reg.exec(text);
The output will be:
'Stack'.
I want receive all the name, but this name is dinamically, sometimes there are just one word, sometime 5, sometime 2.. etc
Possible?
Thanks.
I'd say that isn't a scenario where a regex should be used, but something like this instead:
var str = 'Companie: Stack over flow';
var name = str.split('Companie:')[1].trim();
alert(name);
michael has a good answer, but if you want a regex for that, you can go with
var text = "Companie: 'Stack over flow';"
var reg = /^Companie: '(.*)';$/
var match = reg.exec(text)[1];
The value of match is
"Stack over flow"
I would like to extract the following String :
http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
from this String :
https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
And before, extract, i would like to check if the global String contains more than one time "http" to be sure to extract the jpg only when needed.
How can i do that ?
Extract the data like this:
var myStr = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var splittedStr = myStr.split("-");
var extractedStr = splittedStr[3].slice(1);
To find out how many "http" is present in the string:
var count = (myStr.match(/http/g)).length;
Hopes it helps
var source = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var temp = source.replace("https","http").split("http");
var result = 'http'+temp[2];
use split()
var original = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg";
original = original.split('-/');
alert($(original)[original.length-1]);
your require URL shows in alert dialog
You can use regex :
str.match(/(http?:\/\/.*\.(?:png|jpg))/i)
FIDDLE
I have a javascript string like "firstHalf_0_0_0" or secondHalf_0_0_0". Now I want to get the string before the string "Half" from above both strings using javascript.Please help me.
Thanks.
var myString = "firstHalf_0_0_0";
var parts = myString.split("Half");
var thePart = parts[0];
var str = 'firstHalf_0_0_0',
part = str.match(/(\w+)Half/)[1];
alert(part); // Alerts "first"
var str = "firstHalf.....";
var index = str.indexOf("Half");
var substring = str.substr(0, index);
jsFiddle demo.
Using this you can get any particular part of string.
var str= 'your string';
var result = str.split('_')[0];
Working example here for your particular case.
http://jsfiddle.net/7kypu/3/
cheers!
everyone. I've got a string looks like
var s = "2qf/tqg4/ad(d=d,s(f)d)"
And I've got another string
var n = "abc = /fd/dsf/sdf/a.doc, "
What I want to do is insert n after the first '('
So it will look like
"2qf/tqg4/ad(abc = /fd/dsf/sdf/a.doc, d=d,s(f)d)"
Just use the replace function:
var result = s.replace("(", "("+n);
This barely needs REs.
var t = s.replace(/\(/, '('+n);
This doesn't need REs at all, as String.replace takes strings as well as REs to specify what should be replaced.
var t = s.replace('(', '('+n);
I'm trying to get everything before/after a comma from a string
var test = 'hello,world';
Result:
var one = 'hello';
var two = 'world';
What would be a good way to this?
Thanks
.split
Extra text because I need to write 15 characters for this submission to be approved.
-- edit
okay, more explicitly:
var k = "a,b".split(",");
alert(k[0]);
alert(k[1]);
var test = 'hello,world',
words = test.split(',');
var one = words[0]; // hello
var two = words[1]; // world