Matching everything after first pipe character in Javascript RegEx - javascript

I have this string:
"http://www.yahoo.com/abc/123|X|Y|Z"
I need to get everything after the first pipe with a regex. So I would want to be left with this string:
"X|Y|Z"
How do I do this in JavaScript?

Using a simpler regex /\|(.*)/
var str = "http://www.yahoo.com/abc/123|X|Y|Z";
var aryMatches = str.match(/\|(.*)/);
// aryMatches[1] will have your results
regex explaination

Converting my comment to an answer:
Shockingly, regexes are not the answer to everything.
> xkcd
Try this:
str.split("|").slice(1).join("|");
This splits your string on pipe characters, slices off the first item, then joins the rest with pipes again.

First group contains the expected characters,
^.*?\|(.*)$
DEMO

You can use this regex:
/^[^|]*\|(.*)/
And use matched group #1 for your match.

Forget the splitting and splicing, just use a substring
var str = "http://www.yahoo.com/abc/123|X|Y|Z";
str.substr(str.indexOf("|") + 1);

Related

String operation in javascript

How can I get only 'ABCD' from string 'ABCD150117T15' in java script.
I would like strip rest of the string from 'ABCD' in this example and generally everything until but excluding the first number character.
thanks
You can match for a-z
'ABCD150117T15'.match(/^[a-z]+/i)
you can match anything that is not a number
'ABCD150117T15'.match(/^[^\d]+/)
Use regex then to match the first digit, and then subtring it to the text;
var str = 'ABCD150117T15';
var index = str.search(/\d/);
var text = str.substr(0,index);
'ABCD150117T15'.match(/^\D+/)[0]
This would give you everything until the first number then :)

How to get youku video id from url by regex?

I need to get youku video id from url by regex, for example:
http://v.youku.com/v_show/id_XNTg3OTc3MzY4.html
I only need XNTg3OTc3MzY4 to keep in a variable.
How can I write it in function below
var youkuEmbed = "[[*supplier-video]]";
var youkuUrl = youkuEmbed.match(/http://v\.youku\.com/v_show/id_(\w+)\.html/);
I tried this but it didn't work.
Thanks!
You can use a simple regex like this:
id_(\w+)
Working demo
The idea is to match the _id and the capture all the alphanumeric strings.
MATCH 1
1. [29-42] `XNTg3OTc3MzY4`
If you go the Code Generator section you can get the code. However, you can use something like this:
var myString = 'http://v.youku.com/v_show/id_XNTg3OTc3MzY4.html';
var myRegexp = /id_(\w+)/;
var match = myRegexp.exec(myString);
alert(match[1]);
//Shows: XNTg3OTc3MzY4
You can use this regex:
http://v\.youku\.com/v_show/id_(\w+)\.html
Your match is in the first capturing group.
Here is a regex demo.
Id the id always follows id_, you could possibly split the string.
'http://v.youku.com/v_show/id_XNTg3OTc3MzY4.html'.split(/.*id_|\./)[1]
//=> 'XNTg3OTc3MzY4'
For this specific string, you could just do.
'http://youku.com/id_XNTg30Tc3MzY4.html'.split(/id_|\./)[2]
//=> 'XNTg3OTc3MzY4'
It looks like you need to escape all the slashes because that's the delimiter for the regex itself:
var youkuUrl = youkuEmbed.match(/http:\/\/v\.youku\.com\/v_show\/id_(\w+)\.html/);
Then use the first capture group, as Unihedron stated.

Extract specific chars from a string using a regex

I need to split an email address and take out the first character and the first character after the '#'
I can do this as follows:
'bar#foo'.split('#').map(function(a){ return a.charAt(0); }).join('')
--> bf
Now I was wondering if it can be done using a regex match, something like this
'bar#foo'.match(/^(\w).*?#(\w)/).join('')
--> bar#fbf
Not really what I want, but I'm sure I miss something here! Any suggestions ?
Why use a regex for this? just use indexOf to get the char at any given position:
var addr = 'foo#bar';
console.log(addr[0], addr[addr.indexOf('#')+1])
To ensure your code works on all browsers, you might want to use charAt instead of []:
console.log(addr.charAt(0), addr.charAt(addr.indexOf('#')+1));
Either way, It'll work just fine, and This is undeniably the fastest approach
If you are going to persist, and choose a regex, then you should realize that the match method returns an array containing 3 strings, in your case:
/^(\w).*?#(\w)/
["the whole match",//start of string + first char + .*?# + first string after #
"groupw 1 \w",//first char
"group 2 \w"//first char after #
]
So addr.match(/^(\w).*?#(\w)/).slice(1).join('') is probably what you want.
If I understand correctly, you are quite close. Just don't join everything returned by match because the first element is the entire matched string.
'bar#foo'.match(/^(\w).*?#(\w)/).splice(1).join('')
--> bf
Using regex:
matched="",
'abc#xyz'.replace(/(?:^|#)(\w)/g, function($0, $1) { matched += $1; return $0; });
console.log(matched);
// ax
The regex match function returns an array of all matches, where the first one is the 'full text' of the match, followed by every sub-group. In your case, it returns this:
bar#f
b
f
To get rid of the first item (the full match), use slice:
'bar#foo'.match(/^(\w).*?#(\w)/).slice(1).join('\r')
Use String.prototype.replace with regular expression:
'bar#foo'.replace(/^(\w).*#(\w).*$/, '$1$2'); // "bf"
Or using RegEx
^([a-zA-Z0-9])[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+#([a-zA-Z0-9-])[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$
Fiddle

Is it possible to cut off the beginning of a string using regex?

I have a string which contains a path, such as
/foo/bar/baz/hello/world/bla.html
Now, I'd like to get everything from the second-last /, i.e. the result shall be
/world/bla.html
Is this possible using a regex? If so, how?
My current solution is to split the string into an array, and join its last two members again, but I'm sure that there is a better solution than this.
For example:
> '/foo/bar/baz/hello/world/bla.html'.replace(/.*(\/.*\/.*)/, "$1")
/world/bla.html
You can also do
str.split(/(?=\/)/g).slice(-2).join('')
> '/foo/bar/baz/hello/world/bla.html'.match(/(?:\/[^/]+){2}$/)[0]
"/world/bla.html"
Without regular expression:
> var s = '/foo/bar/baz/hello/world/bla.html';
> s.substr(s.lastIndexOf('/', s.lastIndexOf('/')-1))
"/world/bla.html"
I think this will work:
var str = "/foo/bar/baz/hello/world/bla.html";
alert( str.replace( /^.*?(\/[^/]*(?:\/[^/]*)?)$/, "$1") );
This will allow for there being possibly only one last part (like, "foo/bar").
You can use /(\/[^\/]*){2}$/ which selects a slash and some content twice followed by the end of the string.
See this regexplained.

Simple javascript regex

I need: www.mydomain.com:1235 form the text var below:
var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/[^]+\//));
output is: //www.mydomain.com:1235/
How do I exclude the delimiters?
You need to use parens to group what you want to match. Then, the call to .match() will let you use indexers. Index 0 is the whole string match, and index 1 is the first paren grouping.
var text = 'http://www.mydomain.com:1235/;image.jpg';
alert(text.match(/\/([^\/]+)\//)[1]);
Not a regex, but you could do this:
Example: http://jsfiddle.net/nTmv9/
text = text.split('http://')[1].split('/')[0];
or with a regex:
Example: http://jsfiddle.net/nTmv9/1/
text = text.match(/http:\/\/([^\/]+)\//)[1];
This will capture the domain without the http or the url slugs.
https?:\/\/([^\/]+)\/
If you need help figuring out regex here is a great tool I use all of the time.
http://gskinner.com/RegExr/
Cheers

Categories

Resources