Cant make regex work to remove the first white space character - javascript

I have the following div:
<div data-test="([1] Hello World), ([2] Foo Bar)"></div>
Now what I am trying to do is to find the cleanest way to break the string into the following pieces:
array ["1", "Hello World", "2", "Foo Bar"];
How can I achieve this the proper and fast way?
I managed to get close but my solution seems somewhat ugly and doesnt work as expected.
var el = document.getElementsByTagName("div")[0];
data = el.getAttribute('data-test');
list = data.replace(/[([,]/g, '').split(/[\]\)]/);
for(str of list) {
str = str.trim();
}
I still get the spaces at the start of each string. I dont really want to use trim or anything similar. I tried to add a whitespace character to my regex s/ but that was a bad idea too.

The below function should work.
function strToArr(str) {
var arr = [];
var parts = str.split(', ');
parts.forEach(part => {
var digit = part.match(/(\d+)/g)[0];
var string = part.match(/(\b[a-zA-Z\s]+)/g)[0];
arr.push(digit, string);
});
return arr;
}

var text = '([1] Hello World), ([2] Foo Bar)';
var textReplaced = text.replace(/\(\[([^\]])\]\s([^)]+)\)/g, '$1, $2');
var array = textReplaced.split(', ');
console.log(array);
Without any cycle.

You can try the following regular expression:
list = data.replace(/^\(\[|\)$/g, '').split(/\] |\), \(\[|\] /);
Two steps:
remove the heading "(["and tailing ")"
split the string into the parts you want with the delimiter symbols
Suppose the format of the string is fixed.

Related

JS turn a multi-line string into an array (each item= a line)

For example, I have:
var str = "Hello
World"
I'm expecting an array like that : array["Hello", "World"]
I looked for a method that does that but nothing, I tried to make a loop but I don't know on what I should base my loop? From my knowledge there's not a .length property for the amount of lines in a string...
Use the split function:
var str = `Hello
World`;
var splittedArray = str.split(/\r?\n/);
console.log(splittedArray)
First thing is that the input string is not valid. It should be enclosed by backtick not with a quotes and then you can replace the new line break with the space and then split it to convert into an array.
Live Demo :
var str = `Hello
World`;
const replacedStr = str.replace(/\n/g, " ")
console.log(replacedStr.split(' '));

Extending a regex to capture multiple conditions

Currently have the following regex to capture all content within square brackets:
regex = /[^[\]]+(?=])/g
Meaning that:
string = "[Foo: Bar] [Biz: Baz]"
string.match(regex)
In JavaScript will return: ["Foo: Bar", "Biz: Baz"]
for a next step, I want to only get the text that follows a the colon. It is safe to assume that on all matches, we'll consistently have a return where each string in the return array matches the above pattern.
I'm sure there's some way to extend my regex to do this at the same time as finding the text within square brackets, but I'm just not sure how to do so. I've tried using some positive look-aheads, but I have no idea where to add them.
Another simple way:
const regex = /\[(\w+)\s*:\s*(\w+)\]/g;
const string = "[Foo: Bar] [Biz: Baz]";
let match;
while(match = regex.exec(string)){
console.log(`Pro: ${match[1]}`)
console.log(`Val: ${match[2]}`)
}
You can add :) or (: ) if you need also to match the space after the colon):
var string = "[Foo: Bar] [Biz: Baz]"
var regex = /[^[\]:]+(?=])/g;
console.log(string.match(regex));
You can try something like this
\[([^:]+:\s*)([^\]]+)
let regex = /\[([^:]+:\s*)([^\]]+)\]/g
let arr = []
let string = "[Foo: Bar] [Biz: Baz]"
while((arr =regex.exec(string))!== null){
console.log(`key -> ${arr[1]}`)
console.log(`val -> ${arr[2]}`)
}

Javascript: split a string according to two criteria?

I am trying to count the number of sentences in a paragraph. In the paragraph, all sentences end with either ''.'' or ''!''.
My idea is to first split the paragraph into strings whenever there's a ''.'' or ''!'' and then count the number of splitted strings.
I have tried
.split('.' || '!')
but that does not work. It only splits strings whenever there is a ''.''
May I know how to deal with this?
Just use a Regexp, it's pretty simple ;)
const example = 'Hello! You should probably use a regexp. Nice isn\'t it?';
console.log(example.split(/[.!]/));
You will need to use a regex for this.
The following should work:
.split(/\.|!/)
You can use regex /\.|!/ in split() as str.split(/\.|!/) :
var str = 'some.string';
console.log(str.split(/\.|!/));
str = 'some.string!name';
console.log(str.split(/\.|!/));
const sampleString = 'I am handsome. Are you sure?! Just kidding. Thank you.';
const result = sampleString.split(/\.|!/)
console.log(result);
// to remove elements that has no value you can do
const noEmptyElements = result.filter(str => str);
console.log(noEmptyElements);
Try below code it will give you an exact count of sentences in the paragraph.
function count(string,char) {
var re = new RegExp(char,"gi");
return string.match(re).length;
}
function myFunction() {
var str = 'but that! does! not work. It only splits strings whenever there is a. ';
console.log(count(str,'[.?!]'));
}

replace characters in a string using an associative array mapping

I have an associative array/object such at this:
mymap = {'e':'f', 'l':'g'};
And I want to replace all matching characters in a string using the above as a simple cypher, but only replacing existing characters. As an example,
input = "hello world";
output = input.map(mymap); //how can I do this?
//output is "hfggo worgd"
Balancing performance (for large input) and code size are of interest.
My application is replacing unicode characters with latex strings using this map, but I'm happy to stick with the more general question.
The following works:
mymap = {'e':'f', 'l':'g'};
var replacechars = function(c){
return mymap[c] || c;
};
input = "hello world";
output = input.split('').map(replacechars).join('');
although having to split and then join the input seems quite round-about, particularly if this is applied to a wall of text.
Another way would be loop over the object properties and use regex for each replacement:
var input = 'hello world';
var output = '';
for (var prop in mymap) {
if (mymap.hasOwnProperty(prop)) {
var re = new RegExp(prop, 'g');
output = input.replace(re, mymap[prop]);
}
}

removing affix with javascript in a array

where do we start if we want to remove the affix from this sentence meangan menangkan dimenangkan
affix_list = [
'me-an',
'me-kan,
'di-kan
]
string = 'meangan menangkan dimenangkan'
so it will output
output = [
'ang',
'nang'
'menang'
]
You might want to use regular expressions for those replacements. Starting from your affix_list, this should work:
output = affix_list.reduce(function(str, affix) {
var parts = affix.split("-");
var regex = new RegExp("\\b"+parts[0]+"(\\S+)"+parts[1]+"\\b", "g");
return str.replace(regex, "$1")
}, string).split(" ");
Your regexes will look like this:
/\bme(\S+)an\b/g
/\bme(\S+)kan\b/g
/\bdi(\S+)kan\b/g
But note that you will of course need to replace me-kan before me-an, else "menangkan" will become nangk before the me-kan expression can be applied.
You'll need to start with Javascript regular expressions and iterate through the values, retrieving the middle value accordingly. I'll do that first one for you, and you can try out the rest :)
var re = /me(\w+)an/;
var str = "meangan";
var newstr = str.replace(re, "$1");
console.log(newstr);
// outputs ang
Reference: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

Categories