Javascript - Replace '%20' spaces - javascript

var loc_array = document.location.href.split('/');
var linkElement = document.getElementById("waBackButton");
var newT = document.createTextNode(loc_array[loc_array.length-2]);
var repl = newT.replace('%20',' ');
linkElement.appendChild(repl);
Anyone know why this causes the text to not show up?

Why not just do
unescape(document.location.href);

Related

Get all the occurrences with window.find

I want to highlight in a text all the occurrences from a word that I have in my URL.
For the first occurrence everything works fine. But I don't know how to go to the next one.
highlightText: function(urlParams) {
var urlSearch = window.location.search;
var urlParams = new URLSearchParams(urlSearch);
var searchText = urlParams.get('search');
if (window.find(searchText)) {
var el = document.getElementById('collection-content');
text = el.innerHTML;
marked = text.replace(searchText, "<mark>" + searchText + "</mark>");
el.innerHTML = marked;
}
}
I have tried to add a while(window.find(searchText) before the if but it doesn't work, it seems to loop only on the first occurence of my word.
Thanks in advance
If you're not using regex then it'll only replace the first occurrence, you might try this, Also modify the regex as per your needs.
highlightText: function(urlParams) {
var urlSearch = window.location.search;
var urlParams = new URLSearchParams(urlSearch);
var searchText = urlParams.get('search');
if (window.find(searchText)) {
var el = document.getElementById('collection-content');
text = el.innerHTML;
marked = text.replace(
RegExp(`(${searchText})`),
"<mark>" + searchText + " </mark>");
el.innerHTML = marked;
}
}
Note: The match is case sensitive

Regular Expression: how to replace a string (as a variable) that has a digit in it?

var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var path = '&filters[]=price_gte:';
var value = 10;
How do I replace the &filters[]=price_gte:20 part with the new path? (path + value)
I've tried RegExp():
var re = new RegExp(path);
console.log(url.replace(re, ''));
//returns http://mywebsite.com/?&filters[]=price_gte:20
That didn't even work, let alone adding the digit in the regex. Problem is, price_gte is a variable (and so will price_lte be, and whatever other ranges there might come), with a variable digit after it.
Information I have:
url (http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:100)
path (&filters[]=price_gte:)
new value (10)
Desired result:
http://mywebsite.com?&filters[]=price_gte:10&filters[]=price_lte:100
What am I missing?
You were almost there, just needed to escape the brackets:
var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var path = '&filters[]=price_gte:';
var regexPath = '&filters\\[\\]=price_gte:\\d+';
var value = 10;
var re = new RegExp(regexPath);
console.log(url.replace(re, path+value));
Perhaps you meant this
var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var path = 'filters[]=price_gte:';
var value = 10;
var re = /filters\[\]=price_[g|l]te:\d+/;
alert(url.replace(re,path+value))
However a better solution might be
// from http://stackoverflow.com/a/20420424/295783
function replaceUrlParam(url, paramName, paramValue){
var pattern = new RegExp('('+paramName+'=).*?(&|$)')
var newUrl=url
if(url.search(pattern)>=0){
newUrl = url.replace(pattern,'$1' + paramValue + '$2');
}
else{
newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue
}
return newUrl
}
var url = 'http://mywebsite.com/?&filters[]=price_gte:20';
var value=10;
url = replaceUrlParam(url,"filters[]", 'price_gte:'+value);
alert(url)
This seems to work for me:
function replace() {
var url = 'http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:200';
var path = '&filters[]=price_gte:';
var value = 10;
var newStr = url.replace(/&filters\[\]=price_gte:\d+/, "&filters[]=price_gte:" + value);
alert(newStr);
}
This is enough for getting new value
var str = 'http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:100';
var pattern = /price_gte:\d+/;
var new_value = 10;
str = str.replace(patter, 'price_gte:'+new_value);
And Simple Solution will be =>
function replace() {
var url = 'http://mywebsite.com?&filters[]=price_gte:20&filters[]=price_lte:200';
var path = '&filters[]=price_gte:';
var value = 10;
var newStr = url.replace(/(\w*)(price_gte:)(\d*)/i,"price_gte:"+ value);
alert(newStr);
}

Node.js: Replace a second part of a pattern in a text file line-by-line

I've a less file with content like this:
#background: #345602;
#imgBack: #000;
I can read the whole text file into a variable, and latter save the content of the variable modifying the file:
var lessStr = grunt.file.read('./myLessFile.less');
Now I want to change the variable, say, #imgBack to #ff0000. So that, the modified file would look like:
#background: #345602;
#imgBack: #ff0000;
I there any way to do this by regular expression match and replace? Please help.
EDIT
I've code like:
var str = '#black: #000;\n#grayDarker: #222;\n#grayDark: #333;\n#gray: #555;\n#grayLight: #999;';
var varName = '#black';
var replace = '#ab4564';
var regex = '(' + varName + ':\\s*)(?:#[a-z0-9]+)(.*)$';
var re = new RegExp(regex, 'm');
var replaceStr = '$1' + replace;
str.replace(re, replaceStr);
console.log(str);
But it's not working. Have I mistaken something.
I think you mean this,
> var str = "#background: #345602;\n#imgBack: #000;";
> str.replace(/^(#imgBack:\s*#).*$/gm, "$1ff0000;");
'#background: #345602;\n#imgBack: #ff0000;'
Update:
> var str = '#black: #000;\n#grayDarker: #222;\n#grayDark: #333;\n#gray: #555;\n#grayLight: #999;';
undefined
> var varName = '#black';
> var regex = '(' + varName + ':\\s*)(?:#[a-z0-9]+)(.*)$';
undefined
> var re = new RegExp(regex, 'm');
> var replace = '#ab4564';
undefined
> var replaceStr = "$1" + replace;
undefined
> str.replace(re, replaceStr);
'#black: #ab4564\n#grayDarker: #222;\n#grayDark: #333;\n#gray: #555;\n#grayLight: #999;'
(#imgBack:\s*#)(?:\d+)(.*)$
You can use this.Replacement string will be.
$1#ff0000$2.
See demo.
http://regex101.com/r/iO1uK1/6

Javascript regex search until but do not include

I have a problem and I cannot seem to solve it.
I have some URL's like this:
http://www.example.com/test/filter=show
http://www.example.com/test/filter=show/results=true
I want to remove only \/filter=(\w+)(\/?), so only filter=show but do not include the next forward slash. Also filter is just a variable.
What I have tried:
var url = 'http://www.example.com/test/filter=show/results=true';
var param = 'filter';
var oldParam = '\/('+param+'=(.*?))';
var reg = new RegExp(oldParam, "gi");
var result = url.replace(reg, "/");
This removes only filter= and leave out show.
Any help is greatly appreciated.
try it
var url = 'http://www.example.com/test/filter=show/results=true';
var result = url.replace(/filter=([^&\/]*[/]?)/gi, "");
alert(result);
================= UPDATE ==================
var url = 'http://www.example.com/test/filter=show/results=true';
var params = 'filter';
params = params+'=([^&\/]*[/]?)'
var reg = new RegExp(params, "gi");
var result = url.replace(reg, "");
alert(result);
Try look ahead like
'http://www.example.com/test/filter=show/results=true'.replace(/\/filter=.*?(?=\/)/, '')
Ex:
var url = 'http://www.example.com/test/filter=show/results=true';
var param = 'filter';
var oldParam = '\\/'+param+'=.*?(?=\\/)';
console.log(oldParam)
var reg = new RegExp(oldParam, "gi");
var result = url.replace(reg, "");
console.log(result)
Demo: Fiddle
This should suit your needs:
var url = "http://www.example.com/test/filter=show/results=true";
var param = "filter";
var pattern = "/" + param + "=[^/]+";
var regex = new RegExp(pattern, "gi");
var result = url.replace(regex, "");
document.write(result);
Prints:
http://www.example.com/test/results=true
Explanation:
/filter=[^/]+
Edit live on Debuggex

Regex: Removes all HTML Tags and returns just the text?

i want to strip just text values from below html with js.
var Str = "<span style="">MY name is KERBEROS.</span><B>HELLO Everbody</B>"
All text strips with codes that is below;
[^<>]+(?=[<])
But i want to strip just UPPERCASE words. Clresult must be: MY, KERBEROS, HELLO
Thank you already now for your suggestions.
Regards,
Kerberos
Here is your code, gives output: MY,KERBEROS,HELLO
<script>
String.prototype.stripHTML = function()
{
var matchTag = /<(?:.|\s)*?>/g;
return this.replace(matchTag, "");
};
String.prototype.getUpperCaseWords = function()
{
var matchTag1 = /\b([A-Z])+\b/g;
var o = this.match(matchTag1);
return o;
};
var Str = "<span style=''>MY name is KERBEROS.</span><B>HELLO Everbody</B>";
var out1 = Str.stripHTML();
var out2 = out1.getUpperCaseWords();
alert(out2);
</script>
This is another solution in JavaScript using just one line of regex:
String.prototype.stripHTML = function()
{
var matchTag = /<(?:.|\s)*?>/g;
return this.replace(matchTag, "").match(/\b([A-Z])+\b/g);
};
var Str = "<span style=''>MY name is SATYA PRAKASH.</span><B>HELLO Everyone</B>";
var out1 = Str.stripHTML();

Categories