removing affix with javascript in a array - javascript

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

Related

Regex giving incorrect results

Working in Javascript attempting to use a regular expression to capture data in a string.
My string appears as this starting with the left bracket
['ABC']['ABC.5']['ABC.5.1']
My goal is to get each piece of the regular expression as a chunk or in array.
I have reviewed and see that the match function might be a good choice.
var myString = "['ABC']['ABC.5']['ABC.5.1']";
myString.match(/\[/g]);
The output I see is only the [ for each element.
I would like the array to be like this for example
myString[0] = ['ABC']
myString[1] = ['ABC.5']
myString[2] = ['ABC.5.1']
What is the correct regular expression and or function to get the above-desired output?
If you just want to separate them, you can use a simple expression or better than that you can split them:
\[\'(.+?)'\]
const regex = /\[\'(.+?)'\]/gm;
const str = `['ABC']['ABC.5']['ABC.5.1']`;
const subst = `['$1']\n`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
DEMO
You can use this regex with split:
\[[^\]]+
Details
\[ - Matches [
[^\]]+ - Matches anything except ] one or more time
\] - Matches ]
let str = `['ABC']['ABC.5']['ABC.5.1']`
let op = str.split(/(\[[^\]]+\])/).filter(Boolean)
console.log(op)

Regex remove all string start with special character

I have a string look like:
var str = https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none
I want to remove at start ?pid= to end. The result look like:
var str = https://sharengay.com/movie13.m3u8
I tried to:
str = str.replace(/^(?:?pid=)+/g, "");
But it show error like:
Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat
If you really want to do this at the string level with regex, it's simply replacing /\?pid=.*$/ with "":
str = str.replace(/\?pid=.*$/, "");
That matches ?pid= and everything that follows it (.*) through the end of the string ($).
Live Example:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.replace(/\?pid=.*$/, "");
console.log(str);
You can use split
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"
var result = str.split("?pid=")[0];
console.log(result);
You can simply use split(), which i think is simple and easy.
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.split("?pid");
console.log(str[0]);
You may create a URL object and concatenate the origin and the pathname:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
var url = new URL(str);
console.log(url.origin + url.pathname);
You have to escape the ? and if you want to remove everything from that point you also need a .+:
str = str.replace(/\?pid=.+$/, "")
You can use split function to get only url without query string.
Here is the example.
var str = 'https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none';
var data = str.split("?");
alert(data[0]);

Cant make regex work to remove the first white space character

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.

how to replace '[[' with '${' in javascript

I tried to replace [[ with ${.
var str = "it is [[test example [[testing";
var res = str.replace(/[[[]/g, "${");
I am getting the result "it is ${${test example ${${testing" but I want the result "it is ${test example ${testing".
Your regex is incorrect.
[[[]
will match one or two [ and replace one [ by ${.
See Demo of incorrect regular expression.
[ is special symbol in Regular Expression. So, to match literal [,
you need to escape [ in regex by preceding it \. Without it [ is treated as character class.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
// ^^^^
document.write(res);
you want to escape the [ using \
var res = str.replace(/\[\[/g, "${");
Just problem with escape characters.
use \ before [.
var str = "it is [[test example [[testing";
var res = str.replace(/\[\[/g, "${");
If you don't want to use regex
var res = str.split('[[').join('${');
Sample Here:
var str = "it is [[test example [[testing";
var res = str.split('[[').join('${');
document.write(res);

get all but last occurrences of a pattern in javascript

Given the following patterns:
"profile[foreclosure_defenses_attributes][0][some_text]"
"something[something_else_attributes][0][hello_attributes][0][other_stuff]"
I am able to extract the last part using non-capturing groups:
var regex = /(?:\w+(\[\w+\]\[\d+\])+)(\[\w+\])/;
str = "profile[foreclosure_defenses_attributes][0][properties_attributes][0][other_stuff]";
match = regex.exec(str);
["profile[foreclosure_defenses_attributes][0][properties_attributes][0][other_stuff]", "[properties_attributes][0]", "[other_stuff]"]
However, I want to be able to get everything but the last part. In other words, everything but [some_text] or [other_stuff].
I cannot figure out how to do this with noncapturing groups. How else can I achieve this?
Something like?
shorter, and matches from the back if you can have more of the [] items.
var regex = /(.*)(?:\[\w+\])$/;
var a = "something[something_else_attributes][0][hello_attributes][0][other_stuff11][other_stuff22][other_stuff33][other_stuff44]".match(regex)[1];
a;
or using replace, though less performant.
var regex = /(.*)(?:\[\w+\])$/;
var a = "something[something_else_attributes][0][hello_attributes][0][other_stuff11][other_stuff22][other_stuff33][other_stuff44]".replace(regex, function(_,$1){ return $1});
a;
If those really are your strings:
var regex = /(.*)\[/;

Categories