Regexp - from first character to & character - javascript

I have string like this: http://someurl.com?test&lettersg and I would like to mach part from first letter to & (without &, this part only: http://someurl.com?test).

var match = /^[^&]*/.exec("http://someurl.com?test&lettersg")[0];

If its the code you want, look at the source of this JQuery Plugin (URL Parser). Or you can just go ahead and use the plugin.

For URL manipulation I suggest using URI.js.
Your problem can be solved with and without RegExp:
var string = "http://someurl.com?test&lettersg";
console.log(string.match(/^[^&]+/)[0]);
console.log(string.substring(0, string.indexOf('&')));

Related

Javascript Regular Expression for non-image url

In JavaScript, I want to extract a non-image url from a string e.g.
http://example.com
http://example.com/a.png
http://www.example.ccom/acd.php
http://www.example.com/b.jpg etc.
I would like to extract 1st and 3rd (non-image) URLs and ignore 2nd and 4th (image) URLs.
I tried the following which did not work
(https?:)?\/\/?[^\'"<>]+?^(\.(jpe?g|gif|png))
Which is the modification of the following Image URL Regular Expression (RE) to whom I added ^() (for not) for above snippet
(https?:)?//?[^\'"<>]+?\.(jpg|jpeg|gif|png)
Note: The RE in above examples is case-sensitive, if any clue for making RE case-insensitive
You can use a negative lookahead like these examples It will exclude anything with the string
assuming your urls are newline delimited like your example, something like this should work
(?!.*(jpg|jpeg|gif|png).*).*
EDIT: it looks like my example doesn't work, hopefully it is pointing oyu in the right direction at least
first removing the images:
var tmp = text.replace(/https?:\/\/[\S]+\.(png|jpeg|jpg|gif)/gi, '');
and then matching:
var m = tmp.match(/https?:\/\/[\S]+/gi);
console.log(m);

Matching a specific url in javascript

I'm trying to match youtube and vimeo urls on javascript. I'm using regexr to play around and came up with this:
(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+
It works pretty well on regexr, whitespaces aren't included in the match so it would only match this:
http://youtu.be/ssdfsjlfsfsl
And not this:
http://youtu.be/ssdfsjl someword
But when I test it out on javascript it still matches the url with a whitespace and another word beside it:
var x = new RegExp("(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+")
x.test("http://www.youtube.com/watch?v=FvYo3ZgaQ1c&feature=plcp someword")
Not sure why this is happening, I've also tried adding \S or !\s but it doesn't seem to have any effect.
regex.test(string) returns a boolean value of whether one or more matches are found in the string, so it would (and should) return true for "http://www.youtube.com/watch?v=FvYo3ZgaQ1c&feature=plcp someword".
If you want to test that your string is a URL and ONLY a url, add some anchors:
^(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+$
Doesn't look like you're checking for the start / end of the string. Using ^ and $
var x = new RegExp("^(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+$")
It's because you haven't anchored your expression.
^(http|https)://(youtu|www.youtube|vimeo|youtube)\.(be|com)/[A-Za-z0-9\?&=]+$
| |
here, and... here.

How to remove following symbol/special character in string using jquery ¶

How to remove following symbol/special character in string using jquery
¶
I have problem during comparing two strings .
Above mentioned symbol is append when I compute difference.
I also test it in my local application
please check link below.
http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html
var s = "This is a string that contains ¶, a special character.";
s = s.replace(/¶/g, "");
Yields:
"This is a string that contains , a special character."
This will remove all occurrences of the character. No jQuery necessary - just vanilla browser-provided JavaScript.
Some additional details are available at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace.
jQuery isn't needed for this, you can achieve it by using native javascripts replace function:
var myString = "ABC¶DEFG"
alert(myString.replace("¶", ""))
Example fiddle
javascript replace function should work. If it isn't finding the character then try using the unicode or ansi equivalent.
http://www.alanwood.net/demos/ansi.html

Omitting special characters from the string using Jquery

I have to implement some type of pixel for analytic and it requires passing a session Id in the url string. My sessionID contains special characters. It looks something like this BFhGlzT6FBkDr2Zndp0!-1309
I need to remove the (-!) characters from this string, how do I achieve this using jquery? I need to make sure jquery remove those characters before it render otherwise it will not report a visit to analytic.
Guys thanks your help but maybe I need to explain bit further, my pixel code look some what like this "
img src="https://sometime.com/png?org_id=k8vif92&session_
id=T6PtTyRSqhGPYBhp84frwth67n6fL7wcLBFhGlzT6FBkDr2Zndp0!-130901808!1319637471144&m=2&m=2" alt="">
Before this pixel fire off, I need to replace the character in the sessionId string to remove !- and keep in mind session id will change every time there is a new session. I need a code that is generic so it works no matter what session id is, it needs to delete special characters from it.
Try using .replace:
var token = "BFhGlzT6FBkDr2Zndp0!-1309";
token.replace(/[^A-Za-z0-9]/g, "");
this will remove any character that's not a letter or number. More concisely:
token.replace(/\W/g, "");
(this won't replace underscores)
Black-listing ! and - (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[!\-]/g,""));
White-listing alpha-numeric (fiddle):
var url = "BFhGlzT6FBkDr2Zndp0!-1309";
document.write(url.replace(/[^a-z0-9]/ig,""));
var str = "BFhGlzT6FBkDr2Zndp0!-1309".replace("!-","");
fiddle: http://jsfiddle.net/neilheinrich/eYCjX/
Define a regular expression character set that contains all the allowed characters. For example, yours might look like /[a-zA-Z0-9]/. Now invert it with the complementary character set [^a-zA-Z0-9] and remove all those characters with the String.replace method.
mystring = mystring.replace(/[^a-zA-Z0-9]/g, "");
You dont need jquery for this. You can use javascript regex. See this jsfiddle
var code = "BFhGlzT6FBkDr2Zndp0!-1309";
code = code.replace(/[!-]/g,"");
alert(code);

Get part of string?

I have a string formatted like this:
item_questions_attributes_abc123_id
I'm specifically trying to get the abc123 bit. That string can be any alphanumeric of any case. No special characters or spaces.
I'm using jQuery, though I'm certainly fine with using straight javascript if that's the best solution.
If it's always the 4th part of the string you can use split.
var original = 'item_questions_attributes_abc123_id';
var result = original.split('_')[3];
Try this:
var myArray = myString.split("_");
alert(myArray[3]);
use split method of javascript and then use 2nd last index you will have your required data.

Categories

Resources