Why missing one character from my regex? - javascript

I have a very good regex function which call in here:
<input onkeyup="vimeo();">
But the problem is, when the user paste a link in to the input, he get this link:
//player.imeo.com/video/VIDEOID
You see? Missing "v" from vimeo and I can't pass on this problem.
If I delete the "/" character before of "v" I get repeated value, for example:
//player.player.player.player.player.player.player.player.vimeo.com/video/VIDEOID
My replace script looks like this:
var vimeo = function(){
var str2;
$('#url').keyup(function(){
str2 = $(this).val();
$(this).val(str2.replace(/(?:http:\/\/)?(?:www\.)?(?:vimeo\.com)\/(.+)/g, '\/\/player/\.\vimeo\.\com\/video/$1'));
});
};
How can I fix missing "v" without repeat any value?
Update:
Demo link is here:
http://neocsatblog.mblx.hu/addvideos/

The problem is your replacement string. You don't need to escape any characters. The second argument of the replace method takes a string, not another regular expression.
'\/\/player/\.\vimeo\.\com\/video/$1'
You can just do this:
'//player.vimeo.com/video/$1'
I am not sure why you are using a keyup event on your input as well as your url element, however.
I changed your function a bit and it works for me.
EDIT
Since you want to only use one field, you'll need to use onchange to format the url once the user is done editing. I've changed the function to reflect this change. Notice, you don't even need jQuery anymore.
var vimeo = function(elem) {
var str2 = elem.value;
elem.value = str2.replace(/(?:http:\/\/)?(?:www\.)?(?:vimeo\.com)\/(.+)/g, '//player.vimeo.com/video/$1');
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<input onchange="vimeo(this);">

Related

Cutting the String from an input after a certain value with AngularJS

I am trying to get a value from an input (type = text) and with ngChange I want to write a function so that a link like
https://www.youtube.com/user/testbecomes only user/test
or twitter.com/SomeOne ---> Someone
How am I suppose to do that? I tried substring method but It didnt work fine for me
update : it worked with following code
$scope.cutString = function (link){
var s = link;
s = s.substring(s.indexOf("/")+1,s.length);
console.log(s);
this.link = s;
$scope.publisher.facebook = s ;
};
HTML Part ;
<input type="text" class="form-control" id="facebook" data-ng-model="publisher.facebook"
ng-change="cutString(publisher.facebook)">
but now the problem is I have the same for Facebook, Twitter and Youtube, how can I manage them all in one method?
PS: I cant upload whole code since its against my work contract.
You could:
function getUserPath(link) {
var regex = /^((http[s]?|ftp):\/)?\/?([^:\/s]+)\//;
return link.replace(regex, "");
}
And then call getUserPath wherever you need that logic.
With this RegExp you will always get the path after the first "/" that is not part of the "http://".

Is there any way to detect and change the input value format?

I am checking and validating the text area value with a regex
and I want to change the formating like this:
user input:
123456
change to:
12/34/56
can this be done in pure js?
edit:
this is what I did yet:
function changeIt() {
var inputChanger = document.getElementById("id").value.replace(something , something);
document.getElementById("id").value = inputChanger;
}
but no idea how to proceed
Try something like this:
Note: for this code to work, the HTML must come before the JavaScript, but stack overflow apparently re-order's code snippets to always show the JS first (?)
var input = document.getElementById('my-input');
function format() {
// \d matches a digit,
// parenthesis let you use the matched values as $n in the replacement string
input.value = input.value.replace(/(\d\d)(\d\d)(\d\d)/, '$1/$2/$3');
}
// you probably only need one of these, but it doesn't hurt to have both
input.addEventListener('change', format);
input.addEventListener('keyup', format);
<input type="text" id="my-input" />
It can also, of course, be done with some jQuery and maybe a plugin like http://jquery-plugins.net/maskjs-jquery-plugin-to-mask-inputs - but I think it's good to understand what's happening under the hood even if you end up going that way.

Escape metacharacters and using it as string variable for selector

eg.
I have var myid="abc xyz"
then I escape metachars using function and get var x = "#"+escapechars(myid);
which evaluate to #abc\\xyz
Now when I try to do $(x) it doesn't get any element
but when I type $("#abc\\xyz") in watch it gets the element.
I am attaching a screenshot for same scenario.
Problem is : I want to select the element using variable
Thank you.
Here is the jsfiddle for my scenario.
http://jsfiddle.net/9hq4nzvx/3/
This >http://jsfiddle.net/9hq4nzvx/5/ solves the issue.
When we are using string variable that time we only need a single backslash instead of 2.
function escapechars now returns : "#abc\ xyz" which gets the element as needed.
var selection ="abc xyz";//this can be anything else comes from an array.
var x = "#"+escapeStr(selection); //"#abc\\\\\ xyz";
$(x).html("<h1>Hii</h1>");//why don't I get element here?
console.log(x);
alert(x);
$("#abc\\ xyz").append("<h2>bye</h2>");
function escapeStr(str) {//escape special chars from selectors
if (str)
return str.replace(/([ !"#$%&'()*+,.\/:;<=>?#[\\\]^`{|}~])/g, '\\$1');
return str;
}

Converting Javascript hashtag

What I'm trying to do is fetch a single piece of a string without using the hashtag element in the url. I already have a functioning code but it needs altering. So, how do I fetch any part of the url after ?.
Say I have ?fx=shipment+toys/fish-fix-fx/ as my url string; I want the button to show if shipment or fish or fx was my choice of selections for example.
Buttons showing with hastag: http://jsfiddle.net/66kCf/2/show/#iphone
Original JSFiddle (buttons not showing): http://jsfiddle.net/66kCf/2/
I want the iPhone buttons to show if fix was my choice: http://jsfiddle.net/66kCf/2/show/?fx=shipment+toys/fish-fix-fx/
try doing it with .split() and.match() like this...
var keys = window.location.href.split('?');
if (keys[1].match(/(fix|fish|fx)/))
{
$("#linkdiv").append(nextLink);
$("#linkdiv1").append(nextLink);
$("#linkdiv2").append(nextLink);
}
demo button showing : http://jsfiddle.net/LbKmf/show/?fx=shipment+toys/fish-fix-fx/
demo button not showing: http://jsfiddle.net/LbKmf/show/?reigel
Is this what your looking for:
"?fx=shipment+toys/fish-fix-fx/".split(/[\?=+\/-]/g);
window.location.search and split into array for comparisons
explained in How can I get a specific parameter from location.search?
http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
Generally, Javascript doesn't have a built-in functionality for query string parameters. You can use string manipulation on window.location.search to get your parameters out of the URL string. Note that location.search includes the ? character too.
Something like this should do:
var queryString = function () {
// Anonymous function - executed immediately
// get rid of the '?' char
var str = "?fx=shipment+toys/fish-fix-fx/";
var query = str.substring(str.lastIndexOf('=')+1,str.indexOf('/'));
var vars = query.split("+");
for (var i=0;i<vars.length;i++){
console.log(vars[i]);
}
return vars;
} ();

javascript or jquery - get the last 2 characters of href

I need to get the last 2 characters from the href of a link and place them into a string.
I'm sure this is fairly simple but I seem to be struggling.
Here's the link
test
I need to grab the "bb" part of the href.
Presuming link is a reference to the element:
var chars = link.href.substr(-2);
If you need to get the reference to the link, it is best to give the link an ID attribute, e.g. <a href="../mypage/?code=bb" id="myLink">, where myLink is something that describes the link's purpose. You can then do this:
var chars = document.getElementById('myLink').href.substr(-2);
Finally, if what you want is the code parameter from your link, it may be best to parse the URL into parts. If there is a chance that your URL may be more complex that what you've shown, you should do real URL parsing. As Rahul has pointed out in his answer there are some jQuery plugins that perform this function.
try
$(function() {
var res = $('a').attr('href').split(/=/)[1]
alert(res);
});
This will not grab the last two character, but everything after the = sign which works probably more generic. And even if the <center> cannot hold, regex could look like
$(function() {
var href = $('a').attr('href'),
res = /\\?code=(\w+)/.exec(href);
alert(res[1]);
});
var href = $('a').attr('href');
var last2char = href.substr(href.length-2);
You can try for some querystring plugins which might be a better option.

Categories

Resources