How to remove specific strings in a string? - javascript

I have a string path = "foo/value/bar/value2", and I want to extract the value parts. Because the value can be null, I want to do this by removing the rest of the string.
There is no specific pattern, so the path can be anything, for example "foo//value/value2" (in that case, I must remove the "foo//" and then the "/".)
To make things clear, let's write it path = foo + "value" + bar + "value".
foo and bar are known, not null string values, stored in an array array = [foo, bar]
The best solution for me is to separate my string with something like path.split(regex), in order to have an array with the values, but I haven't found how to split a string from multiple sources.

var path = "foo/value/bar/value2";
var array = ['foo', 'bar'];
var reg = new RegExp(array.join("|"),"g");
console.log(path.split(reg));

Related

Find variable substring after known characters inside a string

I have the following RRULE in a string format:
DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000
I want to parse the properties of the string into their own respective variables to use inside form inputs to update. The same RRULE properties are going to show up in every string so I know for example that DTSTART will always be in the string.
I thought about using the string method search by specifying the property by its name and then adding the number of characters to add to get the position of the entity and want and then use .substring()
So for example, if I was trying to extract UNTIL, I could do:
export const parseUntilFromRRule = (rrule: string):Date => {
const posInRRule = rrule.search("UNTIL=");
const until = rrule.substring(posInRRule + 6);
return new Date(until);
};
However, for properties in the middle of the string, where the value's length may vary, this method would not work because I would not know the value of the second parameter to pass into substring
What generalized technique can I use to extract each RRULE property from the string?
I would use string split twice here:
var input = "DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000";
var rrule = input.split("RRULE:")[1].split(";")[0];
console.log(rrule);
You can split by semicolons, then split each result by = if an entry contains a =, and turn the result into an object:
const input = `DTSTART;TZID=America/Toronto:20160909T040000RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=MO,WE,FR;UNTIL=20161202T040000`;
const segments = input.split(';');
const entryKeyValues = Object.fromEntries(
segments.map(
segment => segment.includes('=')
? segment.split('=')
: [segment, '']
)
);
console.log(entryKeyValues);
console.log(entryKeyValues.UNTIL);

Storing the word length in javascript array

I am puzzled as to why I can not store the word length in a javascript array.
I have tried
var i = [];
i["length"] = "ABC";
i["len"+"gth"] = "ABC";
but both aren't accepted in javascript. Can anyone explain it, and is there a way that I can store the word length in an array as above.
Since some asked for more detail. I am creating a list of words that I need to do a lookup at and find the value to display to the user. My list contains for example:
localVars.FunctionDic = [];
localVars.FunctionDic["lastindexof"] = "LastIndexOf(text, textToLocate)";
localVars.FunctionDic["specificindexof"] = "SpecificIndexOf(text, textToLocate, indexNumber)";
localVars.FunctionDic["empty"] = "Empty(text)";
localVars.FunctionDic["length"] = "Length(text)";
everything works except for the "length"
and I am using an array since I need to test if the word a user search for is in my array, and if it is display the value, if it is not, show nothing
It does not work because you are trying to write a string to a property that only allows a number.
From MDN: The length property of an object which is an instance of type Array sets or returns the number of elements in that array. The value is an unsigned, 32-bit integer that is always numerically greater than the highest index in the array.
With the limited details in your question it is hard to tell what you are actually trying to accomplish. It seems like you want to use an array like an object. If that is the case, use an object.
var i = {};
i["length"] = "ABC";
Based on the expected output, I believe you should be using an object not an array.
const FunctionDic = {
lastindexof: "LastIndexOf(text, textToLocate)",
specificindexof: "SpecificIndexOf(text, textToLocate, indexNumber)",
empty: "Empty(text)",
length: "Length(text)",
};
console.log(FunctionDic["lastindexof"]);
console.log(FunctionDic["specificindexof"]);
console.log(FunctionDic["empty"]);
console.log(FunctionDic["length"]);
To store the word length in an array you can do:
var i = ["length"]
If you want to store the length of a word in an array you can do:
var lengthOfHello = "hello".length
var i = [lengthOfHello]
var i = ["ABC"];
console.log(new Array(i[0].length).length);

replace() on variable not working

replace() is not working on a variable I've created representative of a bunch of names I'm deriving from a JSON object in a loop.
I understand strings are immutable in JS. I believe I have ruled that out.
for (object in Object.keys(json)) {
console.log(json[object]["senderProfile"]["name"])
var name_ = String(json[object]["senderProfile"]["name"])
var name = name_.replace(',', '')
names.push(name+"<br>")
}
document.getElementById("json_out").innerHTML = names;
The HTML that is rendered has commas in between each name. Not sure what to make of it.
names is an array. You are implicitly converting the array to a string. By default, array members are separated by comma. Simple example:
console.log('' + [1,2,3])
You can join array members with a custom separator by calling .join:
console.log('' + [1,2,3].join(''))
It may be possible to simplify your code, but not without knowing what the value of json or json[object]["senderProfile"]["name"] is. However, instead of appending <br> to the name, you could use it as the element separator:
var names = Object.keys(json)
.map(key => json[key]["senderProfile"]["name"]);
document.getElementById("json_out").innerHTML = names.join('<br>');

Javascript create a mapping of array values

I am working on a project where I give a user the ability to create their own email templates and insert tags into them as placeholder values that will eventually replaced with content.
The tags are in the format of [FirstName] [LastName]
I am trying to figure out the best approach to create a function that maps these tags to their values.
For example (Psuedo code):
function convertTags(message){
// Convert all instances of tags within the message to their assigned value
'[FirstName]' = FirstNameVar,
'[LastName]' = LastNameVar
// Return the message with all tags replaced
return message;
}
I assume I could do something like the following:
function convertTags(message){
message = message.replace(/[FirstName]/g, FirstNameVar);
message = message.replace(/[LastName]/g, LastNameVar);
return message;
}
I am just trying to come up with a clean way to do this, preferably in an array/mapping style format that I can easily add to.
Any recommendations on achieving this?
You're on the right lines. You just need to generalise your REGEX to match all params, not specifically 'firstname' or some such other hard-coded value.
Let's assume the replacers live in an object, replacers.
var replacers = {
'foo': 'bar',
'something-else': 'foo'
};
And here's our template:
var tmplt = 'This is my template [foo] etc etc [something-else] - [bar]';
For the replacement, we need iterative replacement via a callback:
tmplt = tmplt.replace(/\[[^\}]+\]/g, function(param) { //match all "[something]"
param = param.replace(/\[|\]/g, ''); //strip off leading [ and trailing ]
return replacers[param] || '??'; //return replacer or, if none found, '??'
});
The value of tmplt is now
This is my template bar etc etc foo - ??
Let's say you have an object like this:
var tagMapper: {};
In this object you can add anything you want as key-value pairs, example:
function addTag(key, value){
key = "__prefix__" + key;
tagMapper[key] = value;
}
addTag("key1", "value1");
The difference between an object and an array in javascript is that one uses named indexes while the other uses numbered indexed to set and retrieve data.
Now every time your user adds a new tag, you just add a new key-value pair to this object by calling the addTag function, then to replace those keys in your template just loop over the object as such:
for (var key in tagMapper) {
if (tagMapper.hasOwnProperty(key)) {
template = template.replace(key, tagMapper[key]);
//key here has value "__prefix__key1" and maps to "value1" from our example
}
}
The prefix was added to ensure the script doesn't replace an undesirable string from our template. Your tag format may be sufficient if you are sure the template doesn't contain any [] tags containing the same key as one in the tagMapper object.

producing a word from a string in javascript

I have a string which is name=noazet difficulty=easy and I want to produce the two words noazet and easy. How can I do this in JavaScript?
I tried var s = word.split("=");
but it doesn't give me what I want .
In this case, you can do it with that split:
var s = "name=noazet difficulty=easy";
var arr = s.split('=');
var name = arr[0]; //= "name"
var easy = arr[2]; //= "easy"
here, s.split('=') returns an array:
["name","noazet difficulty","easy"]
you can try following code:
word.split(' ').map(function(part){return part.split('=')[1];});
it will return an array of two elements, first of which is name ("noazet") and second is difficulty ("easy"):
["noazet", "easy"]
word.split("=") will give you an array of strings which are created by cutting the input along the "=" character, in your case:
results = [name,noazet,difficulty,easy]
if you want to access noazet and easy, these are indices 1 and 3, ie.
results[1] //which is "noazet"
(EDIT: if you have a space in your input, as it just appeared in your edit, then you need to split by an empty string first - " ")
Based on your data structure, I'd expect the desired data to be always available in the odd numbered indices - but first of all I'd advise using a different data representation. Where is this string word coming from, user input?
Just as an aside, a better idea than making an array out of your input might be to map it into an object. For example:
var s = "name=noazet difficulty=easy";
var obj = s.split(" ").reduce(function(c,n) {
var a = n.split("=");
c[a[0]] = a[1];
return c;
}, {});
This will give you an object that looks like this:
{
name: "noazert",
difficulty: "easy"
}
Which makes getting the right values really easy:
var difficulty = obj.difficulty; // or obj["difficulty"];
And this is more robust since you don't need to hard code array indexes or worry about what happens if you set an input string where the keys are reversed, for example:
var s = "difficulty=easy name=noazet";
Will produce an equivalent object, but would break your code if you hard coded array indexes.
You may be able to get away with splitting it twice: first on spaces, then on equals signs. This would be one way to do that:
function parsePairs(s) {
return s.split(' ').reduce(
function (dict, pair) {
var parts = pair.split('=');
dict[parts[0]] = parts.slice(1).join('=');
return dict;
},
{}
);
}
This gets you an object with keys equal to the first part of each pair (before the =), and values equal to the second part of each pair (after the =). If a string has multiple equal signs, only the first one is used to obtain the key; the rest become part of the value. For your example, it returns {"name":"noazet", "difficulty":"hard"}. From there, getting the values is easy.
The magic happens in the Array.prototype.reduce callback. We've used String.prototype.split to get each name=value pair already, so we split that on equal signs. The first string from the split becomes the key, and then we join the rest of the parts with an = sign. That way, everything after the first = gets included in the value; if we didn't do that, then an = in the value would get cut off, as would everything after it.
Depending on the browsers you need to support, you may have to polyfill Array.prototype.reduce, but polyfills for that are everywhere.

Categories

Resources