Replace string in JS array without using native replace() method - javascript

I am stuck a bit with replacing string in js array. I am trying to log the arguments to see what is going on but am missing a piece of the puzzle.
fiddle
// - trying to look for substring in array
// - if match is found
// - replace substring without using the native method replace();
var div = $('.insert');
data = ["erf,", "erfeer,rf", "erfer"];
data = data.map(function (x) {
return /""/g.test(x) ? x.replace(/""/g, "") : x
});
function fakeReplace(data, substr, newstr) {
//should show ["erf,", "erfeer,rf", "erfer"];
div.append("data before match replace = " + data);
div.append("\<br>");
div.append("substr = " + substr);
div.append("\<br>");
div.append("newstr = " + newstr);
div.append("\<br>");
return data.split(substr).join(newstr);
}
fakeReplace(data, "erf", "blue");
//should show ["blue,", "blueeer,rf", "blueer"];
div.append("data after fakeReplace is executed = " + data);

You are treating data like a string in your function. You can use map() to return a new array with each element replaced.
function fakeReplace(data, substr, newstr) {
return data.map(function(s) {
return s.split(substr).join(newstr);
})
}

let myString = "Victor";
let splitted = myString.split('');
function replaceManual(a,b){
for(let i = 0; i<= splitted.length-1; i++)
{
for(let j=i; j <=i;j++)
{
if(splitted[j]===a)
{
splitted[j]=b;
return splitted;
}
else
{
break;
}
}
}
}
replaceManual('V','T');
console.log(splitted.toString().replace(/[^\w\s]/gi, ''));

Related

capitalize the first letter of each word in a sentence

hey guy can anybody help me to fix my code so that it does the task shown in the text,
function tad(strg) {
var char = strg.split('-')
for (var i = 1; i < char.length; i++) {
return char[i].charAt(0).toUpperCase() + char[i].slice(1)
}
}
camelize("background-color") == 'backgroundColor'
camelize("list-style-image") == 'listStyleImage'
Assuming you want to replace all word combinations that have a hyphen in it to a camel cased word. You can use a regex with String.replace with a callback function that capitalizes all words after the hyphen.
function camelize (strg) {
return strg.replace(/-(\w)/g, function (match) {
return match[1].toUpperCase();
});
}
camelize("background-color");
// backgroundColor
camelize("z-index");
// zIndex
camelize("list-style-image");
// listStyleImage
JSFIDDLE
Change your function like bellow
function tad(strg) {
var char = strg.split('-')
for (var i = 1; i < char.length; i++) {
char[i] = char[i].charAt(0).toUpperCase() + char[i].slice(1)
}
return char.join('');
}
You are returning before the loop completes its iterations. The best thing here would be to use Array.prototype.reduce like this
function tad(strg) {
return strg.split('-').reduce(function(result, currentStr) {
return result + currentStr.charAt(0).toUpperCase() + currentStr.slice(1);
}, "");
}
console.log(tad("background-color") === "backgroundColor");
# true
console.log(tad("list-style-image") === "listStyleImage");
# true
First of all, you should concatenate results in some variable, instead of returning in loop.
Secondly fo not forget to add first element of array, since your loop is starting from 1.
function camelize(strg) {
var char = strg.split('-'), result = char[0]
for (var i = 1; i < char.length; i++) {
result += char[i].charAt(0).toUpperCase() + char[i].slice(1)
}
return result
}
alert(camelize("background-color"));
alert(camelize("list-style-image"));
Here is fiddle:
http://jsfiddle.net/C78T3/
You return from the whole function in the first iteration of that loop. Instead, you want to do that for every part, and the join the parts together:
function camelize(string) {
return string.split('-').map(function(part, i) {
return i ? part.charAt(0).toUpperCase() + part.slice(1) : part;
}).join("");
}
// or
function camelize(string) {
return string.split('-').reduce(function(m, part) {
return m + part.charAt(0).toUpperCase() + part.slice(1);
});
}
// or
function camelize(string) {
var parts = string.split('-'),
result = ""
for (var i = 1; i < parts.length; i++) {
result += parts[i].charAt(0).toUpperCase() + parts[i].slice(1)
}
return result;
}

How to insert default value in the comma separated strings?

I have group of comma separated strings and if any string is not a numeric value, I need to insert "(1)".
"stack(2),flow,over(4),temp(7)" Here insert default value to flow(1)
"stack(2),flow(3),over(4),temp" Here insert default value to temp(1)
"stack,flow(3),over,temp" Here insert default value to stack(1),over(1),temp(1)
I have validation code to validate and insert default values where needed. Please help me how to insert a default value within parentheses.
javascript function :
var case1 = "stack(2),flow(2),over(4),temp(7)"; // - true
var case2 = "stack(2),flow(3),over(4),temp(k)"; // - false
var case3 = "stack(2),flow(2),over(4),temp(0"; // - false
var case4 = "stack(2),flow(2),over(,temp)"; // - false
var case5 = "stack(2),flow(2),over(4)temp(8)"; // - false
var case6 = "stack(1),flow(7),over,temp"; // - true
var case7 = "stack(1),flow(7),OVER,Temp"; // - true
var case8 = "stack(1),flow(7),over_r,temp_t"; // - true
function testCases(str)
{
var pattern = /^[a-z]+(?:\(\d+\))?(?:,[a-z]+(?:\(\d+\))?)*$/
return pattern.test(str);
}
The above function works for validation in jsfiddle
tl;dr
Use String.prototype.split and String.prototype.join to process each part of your string.
Details
If you want to apply custom fixes to your string, you need to split it in several parts and then process them. Once the job is done, concat all the parts together.
Implementation
Using Array.prototype.map (Warning: not compatible with IE 8 and below):
Demo on JSFiddle.
function testCases(str) {
return str.split(',').map(function(s) {
if (s.match(/^[a-z]+\(\d+\)$/i)) {
// string is valid
return s;
} else {
// you can do processing here based on the failure reason
return s + '(1)';
}
}).join(',');
}
Using a for loop (IE8-compatible):
function testCases(str) {
var parts = str.split(',');
var i = parts.length;
while (i--) {
var s = parts[i];
if (!s.match(/^[a-z]+\(\d+\)$/i)) {
// string is invalid
// you can do processing here based on the failure reason.
parts[i] = s + '(1)';
}
}
return parts.join(',');
}
Unfortunately, JavaScript doesn't have lookbehinds - they'd be very useful here. Instead, we have to cheat:
str = str.replace(/([^)])(,|$)/g,"$1(1)$2");
What this does is capture whatever character comes before the comma or end of string, provided it is not a close-parenthesis. It then inserts the (1) default value in that position.
var add_default = function(str, def) {
str = str.split(',');
for(var i = 0; i < str.length; ++i) {
if(!/\(\d+\)$/.test(str[i]))
str[i] += "(" + def + ")";
}
return str.join(',');
};
http://jsfiddle.net/BhVx3/3/
Here is a possible solution :
function fix(input) {
var r = /^(.*?)(?:\((\d*)\)?|\)?)$/,
input = input.split(','),
output = [],
item;
while (item = input.shift()) {
item = item.match(r);
item = item[1] + '(' + (item[2] || 1) + ')';
output.push(item);
}
return output.join();
}
var s = 'stack(2),over(4),flow,hello(0,kitty2)';
s = fix(s); // "stack(2),over(4),flow(1),hello(0),kitty2(1)"

How to get parameter value inside a string?

Here's a thing i've been trying to resolve...
We've got some data from an ajax call and the result data is between other stuff a huge string with key:value data. For example:
"2R=OK|2M=2 row(s) found|V1=1,2|"
Is it posible for js to do something like:
var value = someFunction(str, param);
so if i search for "V1" parameter it will return "1,2"
I got this running on Sql server no sweat, but i'm struggling with js to parse the string.
So far i'm able to do this by a VERY rudimentary for loop like this:
var str = "2R=OK|2M=2 row(s) found|V1=1,2|";
var param = "V1";
var arr = str.split("|");
var i = 0;
var value = "";
for(i = 0; i<arr.length; ++i){
if( arr[i].indexOf(param)>-1 ){
value = arr[i].split("=")[1];
}
}
console.log(value);
if i put that into a function it works, but i wonder if there's a more efficient way to do it, maybe some regex? but i suck at it. Hopefully somebody may shine a light on this for me?
Thanks!
This seems to work for your specific use-case:
function getValueByKey(haystack, needle) {
if (!haystack || !needle) {
return false;
}
else {
var re = new RegExp(needle + '=(.+)');
return haystack.match(re)[1];
}
}
var str = "2R=OK|2M=2 row(s) found|V1=1,2|",
test = getValueByKey(str, 'V1');
console.log(test);
JS Fiddle demo.
And, to include the separator in your search (in order to prevent somethingElseV1 matching for V1):
function getValueByKey(haystack, needle, separator) {
if (!haystack || !needle) {
return false;
}
else {
var re = new RegExp('\\' + separator + needle + '=(.+)\\' + separator);
return haystack.match(re)[1];
}
}
var str = "2R=OK|2M=2 row(s) found|V1=1,2|",
test = getValueByKey(str, 'V1', '|');
console.log(test);
JS Fiddle demo.
Note that this approach does require the use of the new RegExp() constructor (rather than creating a regex-literal using /.../) in order to pass variables into the regular expression.
Similarly, because we're using a string to create the regular expression within the constructor, we need to double-escape characters that require escaping (escaping first within the string and then escaping within in the created RegExp).
References:
RegExp.
String.match().
This should work for you and it's delimiters are configurable (if you wish to parse a similar string with different delimiters, you can just pass in the delimiters as arguments):
var parseKeyValue = (function(){
return function(str, search, keyDelim, valueDelim){
keyDelim = quote(keyDelim || '|');
valueDelim = quote(valueDelim || '=');
var regexp = new RegExp('(?:^|' + keyDelim + ')' + quote(search) + valueDelim + '(.*?)(?:' + keyDelim + '|$)');
var result = regexp.exec(str);
if(result && result.length > 1)
return result[1];
};
function quote(str){
return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
}
})();
Quote function borrowed form this answer
Usage examples:
var str = "2R=OK|2M=2 row(s) found|V1=1,2|";
var param = "V1";
parseKeyValue(str, param); // "1,2"
var str = "2R=OK&2M=2 row(s) found&V1=1,2";
var param = "2R";
parseKeyValue(str, param, '&'); // "OK"
var str =
"2R=>OK\n\
2M->2 row(s) found\n\
V1->1,2";
var param = "2M";
parseKeyValue(str, param, '\n', '->'); // "2 row(s) found"
Here is another approach:
HTML:
<div id="2R"></div>
<div id="2M"></div>
<div id="V1"></div>
Javascript:
function createDictionary(input) {
var splittedInput = input.split(/[=|]/),
kvpCount = Math.floor(splittedInput.length / 2),
i, key, value,
dictionary = {};
for (i = 0; i < kvpCount; i += 1) {
key = splittedInput[i * 2];
value = splittedInput[i * 2 + 1];
dictionary[key] = value;
}
return dictionary;
}
var input = "2R=OK|2M=2 row(s) found|V1=1,2|",
dictionary = createDictionary(input),
div2R = document.getElementById("2R"),
div2M = document.getElementById("2M"),
divV1 = document.getElementById("V1");
div2R.innerHTML = dictionary["2R"];
div2M.innerHTML = dictionary["2M"];
divV1.innerHTML = dictionary["V1"];
Result:
OK
2 row(s) found
1,2

Append number to a comma separated list

the list looks like:
3434,346,1,6,46
How can I append a number to it with javascript, but only if it doesn't already exist in it?
Assuming your initial value is a string (you didn't say).
var listOfNumbers = '3434,346,1,6,46', add = 34332;
var numbers = listOfNumbers.split(',');
if(numbers.indexOf(add)!=-1) {
numbers.push(add);
}
listOfNumbers = numbers.join(',');
Basically i convert the string into an array, check the existence of the value using indexOf(), adding only if it doesn't exist.
I then convert the value back to a string using join.
If that is a string, you can use the .split() and .join() functions, as well as .push():
var data = '3434,346,1,6,46';
var arr = data.split(',');
var add = newInt;
arr.push(newInt);
data = arr.join(',');
If that is already an array, you can just use .push():
var data = [3434,346,1,6,46];
var add = newInt;
data.push(add);
UPDATE: Didn't read the last line to check for duplicates, the best approach I can think of is a loop:
var data = [3434,346,1,6,46];
var add = newInt;
var exists = false;
for (var i = 0; i < input.length; i++) {
if (data[i] == add) {
exists = true;
break;
}
}
if (!exists) {
data.push(add);
// then you would join if you wanted a string
}
You can also use a regular expression:
function appendConditional(s, n) {
var re = new RegExp('(^|\\b)' + n + '(\\b|$)');
if (!re.test(s)) {
return s + (s.length? ',' : '') + n;
}
return s;
}
var nums = '3434,346,1,6,46'
alert( appendConditional(nums, '12') ); // '3434,346,1,6,46,12'
alert( appendConditional(nums, '6') ); // '3434,346,1,6,46'
Oh, since some really like ternary operators and obfustically short code:
function appendConditional(s, n) {
var re = new RegExp('(^|\\b)' + n + '(\\b|$)');
return s + (re.test(s)? '' : (''+s? ',':'') + n );
}
No jQuery, "shims" or cross-browser issues. :-)

remove value from comma separated values string

I have a csv string like this "1,2,3" and want to be able to remove a desired value from it.
For example if I want to remove the value: 2, the output string should be the following:
"1,3"
I'm using the following code but seems to be ineffective.
var values = selectedvalues.split(",");
if (values.length > 0) {
for (var i = 0; i < values.length; i++) {
if (values[i] == value) {
index = i;
break;
}
}
if (index != -1) {
selectedvalues = selectedvalues.substring(0, index + 1) + selectedvalues.substring(index + 3);
}
}
else {
selectedvalues = "";
}
var removeValue = function(list, value, separator) {
separator = separator || ",";
var values = list.split(separator);
for(var i = 0 ; i < values.length ; i++) {
if(values[i] == value) {
values.splice(i, 1);
return values.join(separator);
}
}
return list;
}
If the value you're looking for is found, it's removed, and a new comma delimited list returned. If it is not found, the old list is returned.
Thanks to Grant Wagner for pointing out my code mistake and enhancement!
John Resign (jQuery, Mozilla) has a neat article about JavaScript Array Remove which you might find useful.
function removeValue(list, value) {
return list.replace(new RegExp(",?" + value + ",?"), function(match) {
var first_comma = match.charAt(0) === ',',
second_comma;
if (first_comma &&
(second_comma = match.charAt(match.length - 1) === ',')) {
return ',';
}
return '';
});
};
alert(removeValue('1,2,3', '1')); // 2,3
alert(removeValue('1,2,3', '2')); // 1,3
alert(removeValue('1,2,3', '3')); // 1,2
values is now an array. So instead of doing the traversing yourself.
Do:
var index = values.indexOf(value);
if(index >= 0) {
values.splice(index, 1);
}
removing a single object from a given index.
hope this helps
Here are 2 possible solutions:
function removeValue(list, value) {
return list.replace(new RegExp(value + ',?'), '')
}
function removeValue(list, value) {
list = list.split(',');
list.splice(list.indexOf(value), 1);
return list.join(',');
}
removeValue('1,2,3', '2'); // "1,3"
Note that this will only remove first occurrence of a value.
Also note that Array.prototype.indexOf is not part of ECMAScript ed. 3 (it was introduced in JavaScript 1.6 - implemented in all modern implementations except JScript one - and is now codified in ES5).
// Note that if the source is not a proper CSV string, the function will return a blank string ("").
function removeCsvVal(var source, var toRemove) //source is a string of comma-seperated values,
{ //toRemove is the CSV to remove all instances of
var sourceArr = source.split(","); //Split the CSV's by commas
var toReturn = ""; //Declare the new string we're going to create
for (var i = 0; i < sourceArr.length; i++) //Check all of the elements in the array
{
if (sourceArr[i] != toRemove) //If the item is not equal
toReturn += sourceArr[i] + ","; //add it to the return string
}
return toReturn.substr(0, toReturn.length - 1); //remove trailing comma
}
To apply it too your var values:
var values = removeVsvVal(selectedvalues, "2");
guess im too slow but here is what i would do
<script language="javascript">
function Remove(value,replaceValue)
{ var result = ","+value+",";
result = result.replace(","+replaceValue+",",",");
result = result.substr(1,result.length);
result = result.substr(0,result.length-1);
alert(result);
}
Remove("1,2,3",2)
</script>
adding , before and after the string ensure that u only remove the exact string u want
function process(csv,valueToDelete) {
var tmp = ","+csv;
tmp = tmp.replace(","+valueToDelete,"");
if (tmp.substr(0,1) == ',') tmp = tmp.substr(1);
return tmp;
}
use splice, pop or shift. depending on your requirement.
You could also have "find" the indexes of items in your array that match by using a function like the one found here : http://www.hunlock.com/blogs/Ten_Javascript_Tools_Everyone_Should_Have
var tmp = [5,9,12,18,56,1,10,42,'blue',30, 7,97,53,33,30,35,27,30,'35','Ball', 'bubble'];
// 0/1/2 /3 /4/5 /6 /7 /8 /9/10/11/12/13/14/15/16/17/ 18/ 19/ 20
var thirty=tmp.find(30); // Returns 9, 14, 17
var thirtyfive=tmp.find('35'); // Returns 18
var thirtyfive=tmp.find(35); // Returns 15
var haveBlue=tmp.find('blue'); // Returns 8
var notFound=tmp.find('not there!'); // Returns false
var regexp1=tmp.find(/^b/); // returns 8,20 (first letter starts with b)
var regexp1=tmp.find(/^b/i); // returns 8,19,20 (same as above but ignore case)
Array.prototype.find = function(searchStr) {
var returnArray = false;
for (i=0; i<this.length; i++) {
if (typeof(searchStr) == 'function') {
if (searchStr.test(this[i])) {
if (!returnArray) { returnArray = [] }
returnArray.push(i);
}
} else {
if (this[i]===searchStr) {
if (!returnArray) { returnArray = [] }
returnArray.push(i);
}
}
}
return returnArray;
}
or
var csv_remove_val = function(s, val, sep) {
var sep = sep || ",", a = s.split(sep), val = ""+val, pos;
while ((pos = a.indexOf(val)) >= 0) a.splice(pos, 1);
return a.join(sep);
}

Categories

Resources