RegExp to strip a variable in ES6-like format - javascript

I'm using an ES6-like variable formatting with the syntax of ${varName}, and while processing them I'm trying to enumerate all unique variables specified, using the following code:
function enumVars(txt) {
var v, names = [];
var reg = /\$\{\s*[a-zA-Z\$_][a-zA-Z0-9\$_]*\s*}/g;
while (v = reg.exec(txt)) {
var svn = v[0].replace(/???/, ''); // stripped variable name;
if (names.indexOf(svn) === -1) {
names.push(svn);
}
}
return names;
}
I haven't been able to figure out the correct RegExp for stripping the variable name from the exec result.
When I use v[0], it gives me ${varName} string, and I need to strip it into just varName, removing leading ${, trailing }, and all white spaces that may reside inside { } around the variable.
The variable is expected to follow the javascript naming convention, which means:
a valid variable starts with a letter, underscore or '$' symbol, followed by any combination of letters, digits, underscores or '$';
leading and trailing spaces around the variable are to be ignored.
In all, we may have a variable returned from exec as ${ _$abc12_$ }, and I need a RegExp for calling replace that would return just _$abc12_$.
Thanks everyone for helping!

Your replace regexp could be
/^\$\{\s*|\s*}$/g
In English, this says "remove both ${... at the beginning, or ...} at the end.
It could be slightly easier to just grab all the strings, and transform them all at once, then filter out duplicates:
function enumVars(txt) {
return txt
// Find all matches of form ${var}
. match(/\$\{\s*[a-z$_][a-z0-9$_]*\s*}/gi)
// Strip off ${ and }, yielding just variable name
. map(function(v) { return v.replace( /^\$\{\s*|\s*}$/g, ''); })
// Filter out duplicates
. filter(function(v, i, a) { return a.indexOf(v) === i; });
}

Related

Convert string with index in brackets to JSON array

I have various strings with numbers in brackets like "[4]Motherboard, [25]RAM" how can I convert such a string to a JSON array (keeping both ids and values) like this:
{"data":[
{"id":"4","item":"Motherboard"},
{"id":"25","item":"RAM"}
]};
I'm tried using split(",") to create the array but I really can't find out how to get the inner data in this case.
You could use a regular expression, which takes the number and the string, and assign it as property to an object.
var string = "[4]Motherboard, [25]RAM",
data = string.split(', ').map(function (a) {
var p = a.match(/^\[(\d+)\](.+)$/);
return { id: p[1], item: p[2] };
});
console.log(data);
Here one way to do it. The pattern \[(\d+?)\](.+) works like this:
(…) is a capture group. Just means whatever matches within the brackets will be a token in the result.
\d means a digit
\d+ means a digit, one or more times
\d+? means a digit, one or more times, but as few as possibile before the pattern matches something else.
.+ means any character, one or more times.
[ and ] have a special meaning in regular expression, so if you actually want to match the characters themselves, you need to escape them like so \[ and \].
The double backslashes \\ are just a JS oddity when defining a regex via a string as opposed to using a /literal/. Just two ways of saying the same thing.
There's plenty of resources to learn regex syntax, and http://regex101.com is a great place to play with patterns and experiment.
var input = "[4]Motherboard, [25]RAM";
var pattern = '\\[(\\d+?)\\](.+)';
var result = input.split(',').map(function (item) {
var matches = item.match(new RegExp(pattern));
return {id: matches[1], val: matches[2]};
});
console.log(result)
function toArray(string) {
return {
data: string.split(",").map(function(str) {
str = str.trim();
return {
id: str.substring(1, str.indexOf("]")),
item: str.substring(str.indexOf("]") + 1),
};
}),
};
}

Replace leading numbers with Javascript regex

As a follow up to this question (not by me), I need to replace leading numbers of an id with \\3n (where n is the number we're replacing).
Some examples:
"1foo" -> "\\31foo"
"1foo1" -> "\\31foo1"
"12foo" -> "\\31\\32foo"
"12fo3o4" -> "\\31\\32fo3o4"
"foo123" -> "foo123"
Below is a solution that replaces every instance of the number, but I don't know enough regex to make it stop once it hits a non-number.
function magic (str) {
return str.replace(/([0-9])/g, "\\3$1");
}
... Or is regex a bad way to go? I guess it would be easy enough to do it, just looping over each character of the string manually.
Here is a way to achieve what you need using a reverse string + look-ahead approach:
function revStr(str) {
return str.split('').reverse().join('');
}
var s = "12fo3o4";
document.write(revStr(revStr(s).replace(/\d(?=\d*$)/g, function (m) {
return m + "3\\\\";
}))
);
The regex is matching a number that can be followed by 0 or more numbers only until the end (which is actually start) of a reversed string (with \d(?=\d*$)). The callback allows to manipulate the match (we just add reversed \\ and 3. Then, we just reverse the result.
Just use two steps: first find the prefix, then operate on its characters:
s.replace(/^\d+/, function (m) {
return [].map.call(m, function (c) {
return '\\3' + c;
}).join('');
});
No need to emulate any features.
Here is how I would have done it:
function replace(str) {
var re = /^([\d]*)/;
var match = str.match(re)[0];
var replaced = match.replace(/([\d])/g, "\\3$1");
str = str.replace(match, replaced);
return str;
}
document.write(replace("12fo3o4"));
Don't get me wrong: the other answers are fine! My focus was more on readability.

Javascript convert PascalCase to underscore_case/snake_case

How can I convert PascalCase string into underscore_case/snake_case string? I need to convert dots into underscores as well.
eg. convert
TypeOfData.AlphaBeta
into
type_of_data_alpha_beta
You could try the below steps.
Capture all the uppercase letters and also match the preceding optional dot character.
Then convert the captured uppercase letters to lowercase and then return back to replace function with an _ as preceding character. This will be achieved by using anonymous function in the replacement part.
This would replace the starting uppercase letter to _ + lowercase_letter.
Finally removing the starting underscore will give you the desired output.
var s = 'TypeOfData.AlphaBeta';
console.log(s.replace(/(?:^|\.?)([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));
OR
var s = 'TypeOfData.AlphaBeta';
alert(s.replace(/\.?([A-Z])/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));
any way to stop it for when a whole word is in uppercase. eg. MotorRPM into motor_rpm instead of motor_r_p_m? or BatteryAAA into battery_aaa instead of battery_a_a_a?
var s = 'MotorRMP';
alert(s.replace(/\.?([A-Z]+)/g, function (x,y){return "_" + y.toLowerCase()}).replace(/^_/, ""));
str.split(/\.?(?=[A-Z])/).join('_').toLowerCase();
u're welcome
var s1 = 'someTextHere';
var s2 = 'SomeTextHere';
var s3 = 'TypeOfData.AlphaBeta';
var o1 = s1.split(/\.?(?=[A-Z])/).join('_').toLowerCase();
var o2 = s2.split(/\.?(?=[A-Z])/).join('_').toLowerCase();
var o3 = s3.split(/\.?(?=[A-Z])/).join('_').toLowerCase();
console.log(o1);
console.log(o2);
console.log(o3);
Alternatively using lodash:
lodash.snakeCase(str);
Example:
_.snakeCase('TypeOfData.AlphaBeta');
// ➜ 'type_of_data_alpha_beta'
Lodash is a fine library to give shortcut to many everyday js tasks.There are many other similar string manipulation functions such as camelCase, kebabCase etc.
This solution solves the non-trailing acronym issue with the solutions above
I ported the code in 1175208 from Python to JavaScript.
Javascript Code
function camelToSnakeCase(text) {
return text.replace(/(.)([A-Z][a-z]+)/, '$1_$2').replace(/([a-z0-9])([A-Z])/, '$1_$2').toLowerCase()
}
Working Examples:
camelToSnakeCase('thisISDifficult') -> this_is_difficult
camelToSnakeCase('thisISNT') -> this_isnt
camelToSnakeCase('somethingEasyLikeThis') -> something_easy_like_this
"alphaBetaGama".replace(/([A-Z])/g, "_$1").toLowerCase() // alpha_beta_gamma
Problem - Need to convert a camel-case string ( such as a property name ) into underscore style to meet interface requirements or for meta-programming.
Explanation
This line uses a feature of regular expressions where it can return a matched result ( first pair of () is $1, second is $2, etc ).
Each match in the string is converted to have an underscore ahead of it with _$1 string provided. At that point the string looks like alpha_Beta_Gamma.
To correct the capitalization, the entire string is converted toLowerCase().
Since toLowerCase is a fairly expensive operation, its best not to put it in the looping handler for each match-case, and run it once on the entire string.
After toLowerCase it the resulting string is alpha_beta_gamma ( in this example )
This will get you pretty far: https://github.com/domchristie/humps
You will probably have to use regex replace to replace the "." with an underscore.
I found this but I edited it so suit your question.
const camelToSnakeCase = str => str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`).replace(/^_/,'')
Good examples for js:
Snake Case
Kebab Case
Camel Case
Pascal Case
have here
function toCamelCase(s) {
// remove all characters that should not be in a variable name
// as well underscores an numbers from the beginning of the string
s = s.replace(/([^a-zA-Z0-9_\- ])|^[_0-9]+/g, "").trim().toLowerCase();
// uppercase letters preceeded by a hyphen or a space
s = s.replace(/([ -]+)([a-zA-Z0-9])/g, function(a,b,c) {
return c.toUpperCase();
});
// uppercase letters following numbers
s = s.replace(/([0-9]+)([a-zA-Z])/g, function(a,b,c) {
return b + c.toUpperCase();
});
return s;
}
Try this function, hope it helps.
"TestString".replace(/[A-Z]/g, val => "_" + val.toLowerCase()).replace(/^_/,"")
replaces all uppercase with an underscore and lowercase, then removes the leading underscore.
A Non-Regex Answer that converts PascalCase to snake_case
Note: I understand there are tons of good answers which solve this question elegantly. I was recently working on something similar to this where I chose not to use regex. So I felt to answer a non-regex solution to this.
const toSnakeCase = (str) => {
return str.slice(0,1).toLowerCase() + str.split('').slice(1).map((char) => {
if (char == char.toUpperCase()) return '_' + char.toLowerCase();
else return char;
}).join('');
}
Eg.
inputString = "ILoveJavascript" passed onto toSnakeCase()
would become "i_love_javascript"

Return text between brackets and not include brackets

I am using the following regEx to match text in brackets:
'textA(textB)'.match(/\((.+?)\)/g)
But it returns text including the brackets e.g. (textB)
How do I return the text without the brackets e.g. textB
I assume that the input contains balanced parenthesis. If yes, then you could use the below regex to match all characters which are present within the brackets.
[^()]+(?=\))
DEMO
> 'textA(textB)'.match(/[^()]+(?=\))/g)
[ 'textB' ]
Explanation:
[^()]+ Negated character class which matches any character but not of ( or ) one or more times.
(?=\)) positive lookahead which asserts that the matched characters must be followed by a closing parenthesis )
You have to explicitly include the parentheses in the regular expression by quoting them with \
'textA(textB)'.match(/\((.+?)\)/g)
If you don't do that, the outer parentheses are interpreted as regex metacharacters.
To extract the matched text without the surrounding parentheses:
var match = 'textA(textB)'.match(/\((.+?)\)/); // no "g"
var text = match[1];
It's tricky to create a regular expression that works with the "g" ("global") qualifier to match and collect the strings within parentheses, because that qualifier causes the .match() function return value to change. Without "g", the .match() function returns an array with the overall match in position 0 and the matched groups in subsequent positions. However, with the "g", .match() simply returns all matches of the entire expression.
The only way I can think of is to repeatedly match, and the easiest way to do that (in my opinion) is with a function:
var parenthesized = [];
var text = "textA (textB) something (textC) textD) hello (last text) bye";
text.replace(/\((.+?)\)/g, function(_, contents) {
parenthesized.push(contents);
});
That will accumulate the properly-parenthesized strings "textB", "textC", and "last text" in the array. It will not include "textD" because it is not properly parenthesized.
It is possible to define a function which matches a string against a regex and customize the output array via a user-defined function.
String.prototype.matchf = function (re, fun) {
if (re == null || re.constructor != RegExp) {
re = new RegExp(re);
}
// Use default behavior of String.prototype.match for non-global regex.
if (!re.global) {
return this.match(re);
}
if (fun == null) { // null or undefined
fun = function (a) { return a[0]; };
}
if (typeof fun != "function") {
throw TypeError(fun + " is not a function");
}
// Reset lastIndex
re.lastIndex = 0;
var a;
var o = [];
while ((a = re.exec(this)) != null) {
o = o.concat(fun(a));
}
if (o.length == 0) {
o = null;
}
return o;
}
The user-defined function is supplied with an array, which is the return value of RegExp.exec.
The user-defined function is expected to return a value or an array of values. It can return an empty array to exclude the content of the current match from the resulting array.
The behavior of the custom function above should be the same as String.match when user-defined function fun is not supplied. This should have less overhead compared to abusing String.replace to extract an array, since it doesn't have to construct the replaced string.
Back to your problem, using the custom function above, you can write your code as:
'textA(textB)'.matchf(/\((.+?)\)/g, function (a) {return a[1];});
Use the matchAll function instead. Maybe?
(your original regular expression was good enough for this)
for (const results of 'textA(textB)'.matchAll(/\((.+?)\)/g)) {
console.log(results[0]) // outputs: (textB)
console.log(results[1]) // outputs: textB
}
OR
const results = [...'textA(textB)(textC)'.matchAll(/\((.+?)\)/g)];
console.log(results) // outputs array of each result
The matchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups;
index 0 returns the whole, and indexes after return group parts.

javascript variable contains instead of equals

in a javascript if...else statement, instead of checking if a variable equals (==) a value, is it possible to check if a variable includes a value?
var blah = unicorns are pretty;
if(blah == 'unicorns') {}; //instead of doing this,
if(blah includes 'unicorns') {}; //can i do this?
also, the word it includes should be the FIRST word of the variable. Thanks!!!
If by "first word", you mean a character sequence from the beginning of the string to the first space, then this will do it:
if ((sentence + ' ').indexOf('unicorns ') === 0) {
// note the trailing space ^
}
If instead of a space it can be any white-space character, you should use a regular expression:
if (/^unicorns(\s|$)/.test(sentence)) {
// ...
}
// or dynamically
var search = 'unicorns';
if (RegExp('^' + search + '(\\s|$)').test(sentence)) {
// ...
}
You can also use the special word-boundary character, depending on the language you want to match:
if (/^unicorns\b/.test(sentence)) {
// ...
}
More about regular expressions.
Related question:
How to check if a string "StartsWith" another string?
if(blah.indexOf('unicorns') == 0) {
// the string "unicorns" was first in the string referenced by blah.
}
if(blah.indexOf('unicorns') > -1) {
// the string "unicorns" was found in the string referenced by blah.
}
indexOf
To remove the first occurrence of a string:
blah = blah.replace('unicorns', '');
You can also use a quick regex test:
if (/unicorns/.test(blah)) {
// has "unicorns"
}

Categories