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;
}
Related
I'm trying to replace the word hello anywhere on the page with the word hi using Javascript. I created the script below however it isn't working how anticipated, is there something that I can do to achieve the outcome desired.
function(data) {
var newdata = data.replace("hello", "hi");
}
Jsfiddle
This will work, although might be overkill using regular expressions:
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, "hi")
Jsfiddle
In your example, you are only replacing the first occurence.
Below the JavaScript documentation about replace:
Note: If you are replacing a value (and not a regular expression),
only the first instance of the value will be replaced. To replace all
occurrences of a specified value, use the global (g) modifier.
You have to use the global modifier as below
function(data) {
var newdata = data.replace(/hello/g,"hi");
}
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);">
I know the code is very little and I'm missing something small.
fiddle : http://jsfiddle.net/0oa9006e/1/
code :
var veri = "+???+Girdiğiniz eposta adresi 'adfadf' geçersiz.-???-";
var a = veri.match(/\+[\?]*\+(.*)*\-[\?]*\-/g);
a = a.replace(/[\+\-\?]*/g , "");
alert(a);
String.match(param) method returns an Array containing all matches. and array in javascript doesn't have .replace method. hence Error. You could try out something like:
a = a.toString().replace(/[\+\-\?]*/g,""); // Array to string converstion
Your match is returning an array, which doesn't have replace. Try:
a = a[0].replace(/[\+\-\?]*/g , "");
var veri = "+???+Girdiğiniz eposta adresi 'adfadf' geçersiz.-???-";
var a = veri.match(/\+[\?]*\+(.*)*\-[\?]*\-/g);
// The variable 'a' is now an array.
// The first step in debugging is to always make sure you have the values
// you think you have.
console.log(a);
// Arrays have no replace method.
// Perhaps you are trying to access a[0]?
// or did you mean to modify `veri`?
a = a.replace(/[\+\-\?]*/g , "");
alert(a);
When veri.match(/\+[\?]*\+(.*)*\-[\?]*\-/g) is executed, your variable a is initialized to a JavaScript Array, which does not have a replace method.
Use a RegEx tool like Regex101 to see how your regular expression matches on the string veri, and then perform the replace operation on the appropriate element of that array.
Here's an example of your regular expression in use: http://regex101.com/r/hG3uI1/1
As you can see, your regular expression matches the entire string held by veri, so you want to perform the replace operation on the first (and only) element returned by match:
a = a[0].replace(/[\+\-\?]*/g , "");
I need to get a id from a html element and replace a part of the word. For example:
HTML
<input type="checkbox" id="facebookCheckbox"></div>
JavaScript
var x = document.getElementById("facebookCheckbox");
var name = x.id;
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced. Is there a different way of doing this?
I'm looking for purely javascript no jQuery
Thank you!
name.replace("Checkbox","");
This obviously does not work because the replacing word has to be standalone for it to be replaced.
No, it does work and there's no need to be "standalone" - any part of the string can be matched. Only you did nothing with the result of the operation:
console.log(name.replace("Checkbox",""));
// or
name = name.replace("Checkbox","");
// or assign back to x.id maybe?
You are creating a copy of string when replacing, so you must assign the result of .replace() back to x.id.
var x = document.getElementById("facebookCheckbox");
x.id = x.id.replace("Checkbox","");
this is not going to work in this way. However you can have a marker kind of character by which you can break the name into array and implement the logic. For example:
var x = document.getElementById("facebook_Checkbox");
//Note I have added underscore in the Id
var name = x.id;
var arr=name.split("_");
//Now you have Checkbox and Facebook as string objects (part of array) and you can use them
name=arr[0]
I hope it will solve the purpose.
When I have something like this:
var str = "0123";
var i = 0;
str.replace(/(\d)/g,function(s){i++;return s;}('$1'));
alert(i);
Why does "i" equal 1 and not 4?
Also, is it possible to pass the real value of $1 to a function (in this case 0,1,2,3) ?
When you use string.replace(rx,function) then the function is called with the following arguments:
The matched substring
Match1,2,3,4 etc (parenthesized substring matches)
The offset of the substring
The full string
You can read all about it here
In your case $1 equals Match1, so you can rewrite your code to the following and it should work as you desire:
var str = "0123";
var i = 0;
str.replace(/(\d)/g,function(s,m1){i++;return m1;});
alert(i);
The expression
function(s){i++;return s;}('$1')
Creates the function and immediately evaluates it, passing $1 as an argument. The str.replace method already receives a string as its second argument, not a function. I believe you want this:
str.replace(/(\d)/g,function(s){i++;return s;});
You are calling the function, which increments i once, and then returns the string '$1'.
To pass the value to a function, you can do:
str.replace(/\d/g, function (s) { /* do something with s */ });
However, it looks like you don't actually want to replace anything... you just want a count of the number of digits. If so, then replace is the wrong tool. Try:
i = str.match(/\d/g).length;