Replacing all symbols between 2 others - javascript

I have a database connection string that can optionally contain a password, which would be between symbols : and #.
How can I have each password symbol replaced with #, using regular expressions?
P.S. I don't want to change the string's length, only to replace the password symbols.

You can use replace()
var pas = 'ddd:hsjhjshdjhd#dddd:djhfjdfj#';
var res = pas.replace(/:(.+?)#/g, function(_, m) {
var r = ':';
for (i = 0; i < m.length; i++)
r += '#';
return r + '#';
});
document.write(res);
UPDATE : As #Tushar suggested you can avoid the for loop by following method
var str = 'ddd:hsjhjshdjhd#ddddSomethingElse';
// Add global flag `g` for more than one password
var res = str.replace(/:([^#]+)/, function(a, m) {
return ':' + new Array(m.length + 1).join('#');
});
document.write(res);

Related

Regex global replace not replacing first occurrence

I'm trying to make a quick and dirty template test, but when the regular expression replaces, it only replaces the last occurrence, I thought that the \g would replace all copies. but it's only matching the last occurrence.
(How to String.match() distinct it ${SOME_TEXT} using Regex)
What I'm trying to do is prompt the user once for each unique variable name.
Title = t
name = n
result = '${Title} - n - t'
Using /\$\{([^\}]+)\}/g works, but prompts the user multiple times.
Title = t
name = n
Title = t
result = 't - n - t'
So how can I replace each token with a single value no matter how many times it appears.
<html>
<head>
<script type="application/javascript">
window.copyToClipboard = function(n) {
var u = "_hiddenCopyText_", t, i, r;
t = document.getElementById(u);
t.textContent = n;
i = document.activeElement;
t.focus();
t.setSelectionRange(0, t.value.length);
try {
r = document.execCommand("copy")
} catch (f) {
r = !1
}
return i && typeof i.focus == "function" && i.focus(),
t.textContent = "",
r
};
//${varname}
window.templateRegex = /\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g;
window.test = " ${Title} - ${Name} - ${Title}"
window.copyTemplate = function (template) {
var result = template.replace(window.templateRegex, function(match, token){
return window.prompt("replace value for ${"+token+"}","${"+token+"}");
});
window.copyToClipboard(result);
};
</script>
</head>
<textarea id="_hiddenCopyText_"></textarea>
<button onclick="copyTemplate(window.test)">Test</button>
</html>
JsFiddle https://jsfiddle.net/ksu37c3b/
If same ${..}(${Title}) occurs several times, /\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g will only match the last one. To match all the elements, /\$\{([^\}]+)\}/g will work, it doesn't need the negative lookahead (?![\S\s]*\$\{\1\}).
In the string "${Title} - ${Name} - ${Title}", only ${Name} and ${Title} matches the pattern, because the first one and the last one are the same, if you change the string, for example, "${Title2} - ${Name} - ${Title}" it will match all the element, because ${Title2} and ${Title} are not equal. That's what the (?![\S\s]*\$\{\1\}) try to do, if the last ${..} and the current ${..} are the same, it won't match.
update
So how can I replace each token with a single value no matter how many times it appears.
You can use the match() method to get an array of tokens occured, then replace it one by one, the code below will work:
window.templateRegex = /\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g;
window.test = " ${Title} - ${Name} - ${Title}";
var userinput, reg, i;
var arr = window.test.match(/\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g);
for (i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(/[\$\{\}]/g, "");
userinput = window.prompt("replace value for " + arr[i]);
reg = new RegExp("\\$\\{" + arr[i] + "\\}", 'g');
console.log(reg);
window.test = window.test.replace(reg, userinput);
}
alert(window.test);
I have modified your code as follows:
window.copyToClipboard = function(n) {
var u = "_hiddenCopyText_",
t, i, r;
t = document.getElementById(u);
t.textContent = n;
//** snip **//
};
//${varname}
window.templateRegex = /\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g;
window.test = " ${Title} - ${Name} - ${Title}";
window.copyTemplate = function(template) {
var result = template;
var userinput, reg;
var arr = result.match(/\$\{([^\}]+)\}(?![\S\s]*\$\{\1\})/g);
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].replace(/[\$\{\}]/g, "");
userinput = window.prompt("replace value for " + arr[i]);
reg = new RegExp("\\$\\{" + arr[i] + "\\}", 'g');
console.log(reg);
result = result.replace(reg, userinput);
}
window.copyToClipboard(result);
};
<textarea id="_hiddenCopyText_"></textarea>
<button onclick="copyTemplate(window.test)">Test</button>​
This should do what you want /\$\{([^\}]+)\}(?![\S\s]\$\{\1\})/
I just removed the * in the middle of the negative look ahead because it was causing it to match the final occurrence every time.

Regex to remove all characters that are repeated

I'm looking for a regex that will remove all characters that have been repeated in a string. I already solved this using a loop. Just wondering if there is a regex that can do the same.
this is what i have so far:
function onlyUnique(str) {
var re = /(.)(?=.*\1)/g
return str.replace(re, '');
}
This string:
"rc iauauc!gcusa_usdiscgaesracg"
should end up as this:
" !_de"
You can use Array#filter with Array#indexOf and Array#lastIndexOf to check if the element is repeated.
var str = "rc iauauc!gcusa_usdiscgaesracg";
// Split to get array
var arr = str.split('');
// Filter splitted array
str = arr.filter(function (e) {
// If index and lastIndex are equal, the element is not repeated
return arr.indexOf(e) === arr.lastIndexOf(e);
}).join(''); // Join to get string from array
console.log(str);
document.write(str);
well, no idea if regex can do that, but you could work it out using for loop, like:
function unikChars(str) {
store = [];
for (var a = 0, len = str.length; a < len; a++) {
var ch = str.charAt(a);
if (str.indexOf(ch) == a && str.indexOf(ch, a + 1) == -1) {
store.push(ch);
}
}
return store.join("");
}
var str = 'rc iauauc!gcusa_usdiscgaesracg';
console.log(unikChars(str)); //gives !_de
Demo:: jsFiddle
Your regex searches pairs of duplicated characters and only removes the first one. Therefore, the latest duplicate won't be removed.
To address this problem, you should remove all duplicates simultaneously, but I don't think you can do this with a single replace.
Instead, I would build a map which counts the occurrences of each character, and then iterate the string again, pushing the characters that appeared only once to a new string:
function onlyUnique(str) {
var map = Object.create(null);
for(var i=0; i<str.length; ++i)
map[str[i]] = (map[str[i]] || 0) + 1;
var chars = [];
for(var i=0; i<str.length; ++i)
if(map[str[i]] === 1)
chars.push(str[i]);
return chars.join('');
}
Unlike indexOf, searches in the hash map are constant on average. So the cost of a call with a string of n characters will be n.
If you want to do it with a regex, you can use your own regex with a callback function inside a replace.
var re = /(.)(?=.*\1)/g;
var str = 'rc iauauc!gcusa_usdiscgaesracg';
var result = str;
str.replace(re, function(m, g1) {
result = result.replace(RegExp(g1.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"), '');
});
document.getElementById("r").innerHTML = "'" + result + "'";
<div id="r"/>
The idea is: get the duplicated character, and remove it from the input string. Note that escaping is necessary if the character might be a special regex metacharacter (thus, g1.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") is used).
Another idea belongs to Washington Guedes in his deleted answer, I just add my own implementation here (with removing duplicate symbols from the character class and escaping special regex chars):
var s = "rc iauauc!gcusa_u]sdiscgaesracg]";
var delimiters= '[' + s.match(/(.)(?=.*\1)/g).filter(function(value, index, self) { // find all repeating chars
return self.indexOf(value) === index; // get unique values only
}).join('').replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + ']'; // escape special chars
var regex = new RegExp(delimiters, 'g'); // build the global regex from the delimiters
var result = s.replace(regex, ''); // obtain the result
document.getElementById("r2").innerHTML = "'" + result + "'";
<div id="r2"/>
NOTE: if you want to support newline symbols as well, replace . with [^] or [\s\S] inside the regex pattern.
function onlyUnique(str) {
// match the characters you want to remove
var match = str.match(/(.)(?=.*\1)/g);
if (match) {
// build your regex pattern
match = '[' + match.join('') + ']';
}
// if string is already unique return the string
else {
return str
}
// create a regex with the characters you want to remove
var re = new RegExp(match, 'g');
return str.replace(re, '');
}

Javascript slug function with non-latin chars

I am writing a function to make slug from input.
var vslug = function (str) {
str = str.replace(/^\s+|\s+$/g, '');
str = str.toLowerCase();
var vregex = /(?:\.([^.]+))?$/;
var filename = str.replace(vregex.exec(str)[0],'');
var extension = vregex.exec(str)[1];
var from = "àáäâèéëêìíïîıòóöôùúüûñçşğ·/,:;";
var to = "aaaaeeeeiiiiioooouuuuncsg_____";
for (var i = 0; i < from.length; i++) {
console.log('before ' + str);
str = filename.replace(new RegExp(from[i], 'g'), to[i]);
console.log('after ' + str);
}
str = str.replace(/[^a-z0-9 _-]/g, '')
.replace(/\s+/g, '_')
.replace(/-+/g, '_');
if (typeof extension !== "undefined") {
return str+'.'+extension;
} else {
return str;
}
};
I can't make this part - I gone blind. Any help is appreciated..
var from = "àáäâèéëêìíïîıòóöôùúüûñçşğ·/,:;";
var to = "aaaaeeeeiiiiioooouuuuncsg_____";
for (var i = 0; i < from.length; i++) {
console.log('before ' + str);
str = filename.replace(new RegExp(from[i], 'g'), to[i]);
console.log('after ' + str);
}
filename is not changed - the variable names the same string, and the string cannot be modified. As such, each loop starts working on the original string again when it uses filename.replace...
Instead, eliminate filename (or integrate it fully) and use str = str.replace..
str = str.replace(vregex.exec(str)[0],'');
for (var i = 0; i < from.length; i++) {
str = str.replace(new RegExp(from[i], 'g'), to[i]);
// ^-- next loop gets new value
}
(Also, this could be handled with a replacement function and a map instead of n-loops and there might be a Unicode library for JavaScript available..)
An approach using a map and a replacement function might look like:
// Specify map somewhere reusable; can be built from paired arrays for simplicity.
var replacements = {"à":"a", "á":"a", .. ";":"_"}
// Object.keys is ES5, shim as needed. e.g. result: [à;á..]
var alternation = "[" + Object.keys(replacements).join("") + "]"
// This regex will match all characters we are trying to match.
var regex = new Regex(alternation, "g")
str = str.replace(regex, function (m) {
var r = replacements[m]
return r || m
})
See String.replace(regex, function)

remove all empty values from url

I want to remove all empty values from an url:
var s="value1=a&value2=&value3=b&value4=c&value5=";
s = s.replace(...???...);
alert(s);
Expected output:
value1=a&value3=b&value4=c
I only need the query part of the URL to be taken into account.
Something like this:
s = s.replace(/[^=&]+=(&|$)/g,"").replace(/&$/,"");
That is, remove groups of one or more non-equals/non-ampersand characters that are followed by an equals sign and ampersand or end of string. Then remove any leftover trailing ampersand.
Demo: http://jsfiddle.net/pKHzr/
s = s.replace(/[^?=&]+=(&|$)/g,"").replace(/&$/,"");
Added a '?' to nnnnnn's answer to fix the issue where the first parameter is empty in a full URL.
This should do the trick:
var s="value1=a&value2=&value3=b&value4=c&value5=";
var tmp = s.split('&')
var newS = '';
for(var i in a) {
var t = a[i];
if(t[t.length - 1] !== '=') {
newS += t + '&';
}
}
if(newS[newS.length - 1] === '&') {
newS = newS.substr(0, newS.length - 1);
}
console.log(newS);
I don't find any solution to do that with one Regex expression.
But you could loop through your string and construct a new result string : http://jsfiddle.net/UQTY2/3/
var s="value1=a&value2=&value3=b&value4=c&value5=";
var tmpArray = s.split('&');
var final = '';
for(var i=0 ; i<tmpArray.length ; i++)
if(tmpArray[i].split('=')[1] != '')
final += tmpArray[i] + '&';
final = final.substr(0,final.length-1)
alert(final)
Where do you take all the values?
I suggest using an array:
function getValues(str){
var values = [];
var s = str.split('&');
for(var val in s){//source is a
var split = val.split('=');
if(split [1] != '' && split [1] != null){
values.push(val);
}
}
return values.join('&');
}

Unicode value \uXXXX to Character in Javascript

I've never done this before and am not sure why it's outputting the infamous � encoding character. Any ideas on how to output characters as they should (ASCII+Unicode)? I think \u0041-\u005A should print A-Z in UTF-8, which Firefox is reporting is the page encoding.
var c = new Array("F","E","D","C","B","A",9,8,7,6,5,4,3,2,1,0);
var n = 0;
var d = "";
var o = "";
for (var i=16;i--;){
for (var j=16;j--;){
for (var k=16;k--;){
for (var l=16;l--;){
d = c[i].toString()
+ c[j].toString()
+ c[k].toString()
+ c[l].toString();
o += ( ++n + ": "
+ d + " = "
+ String.fromCharCode("\\u" + d)
+ "\n<br />" );
if(n>=500){i=j=k=l=0;} // stop early
}
}
}
}
document.write(o);
The .fromCharCode() function takes a number, not a string. You can't put together a string like that and expect the parser to do what you think it'll do; that's just not the way the language works.
You could ammend your code to make a string (without the '\u') from your hex number, and call
var n = parseInt(hexString, 16);
to get the value. Then you could call .fromCharCode() with that value.
A useful snippet for replacing all unicode-encoded special characters in a text is:
var rawText = unicodeEncodedText.replace(
/\\u([0-9a-f]{4})/g,
function (whole, group1) {
return String.fromCharCode(parseInt(group1, 16));
}
);
Using replace, fromCharCode and parseInt
If you want to use the \unnnn syntax to create characters, you have to do that in a literal string in the code. If you want to do it dynamically, you have to do it in a literal string that is evaluated at runtime:
var hex = "0123456789ABCDEF";
var s = "";
for (var i = 65; i <= 90; i++) {
var hi = i >> 4;
var lo = i % 16;
var code = "'\\u00" + hex[hi] + hex[lo] + "'";
var char = eval(code);
s += char;
}
document.write(s);
Of course, just using String.fromCharCode(i) would be a lot easier...

Categories