Dynamic making Regexp [duplicate] - javascript

This question already has answers here:
Javascript Regexp dynamic generation from variables? [duplicate]
(4 answers)
Closed 7 years ago.
I'm looking for a working solution with dynamic regex in Javascript.
this solution works for me: (but is not dynamic)
new RegExp(\bal\i);
but this solution is not working:
var value = 'bal';
new RegExp('\'+value+'\i');
Could anyone help me how to adjust it to make it work? Thank you

you can pass the string (value) in the RegExp constructor, along with the ignoreCase flag as:
var value = 'bal';
var b = new RegExp(value, 'i')
b.test('BAL')
it returns true.

Related

How to mutate this in a new String function? [duplicate]

This question already has answers here:
Are JavaScript strings immutable? Do I need a "string builder" in JavaScript?
(10 answers)
Closed 7 years ago.
I am still getting used to Javascript (I'm coming from C++) and would like to add a new function to String which mutates the string instance directly. Let's say that I want to add a new character at the midpoint of the string (ignoring any error checking). In C++ you could do something like this->value = .... Is that the way to do this in Javascript? TIA
String.prototype.mutateSelf = function(param1) {
// How do I mutate this specific string instance?
return this;
};
Javascript strings are immutable. You must construct a new string and return it.

Tricking Javascript variable [duplicate]

This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

Reversing a variable in javascript [duplicate]

This question already has answers here:
How do you reverse a string in-place in JavaScript?
(57 answers)
Closed 8 years ago.
I don't really have a good way to explain in words what i wont to do.So im just going to have an example.
this is what the variable would be before.
var foo ="foo";
this is what i wont it be after.
var foo ="oof";
I hope that you under stand what i'm asking!
Thinks !
Try this:
var foo="start".split("").reverse().join("");

javascript RegExp pattern change after used with test() method [duplicate]

This question already has answers here:
Why does a RegExp with global flag give wrong results?
(7 answers)
Closed 9 years ago.
I have this simple pattern that validate Time - 24 Hours Mode -
var patt = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/g;
If i execute this pattern with .test() method twice i get two different values
Like This
CODE
console.log(patt.test('01:09')); // true
console.log(patt.test('01:09')); // false
i have notice that if i do the following i get the same result :
var patt = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/g;
console.log(patt.test('01:09')); // true
var patt = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/g;
console.log(patt.test('01:09'));//true
Question
i'll re-initiate the patt object as a work around in my case but i'm very curious to know what happens
Also i notice that patt object doesn't change after using it with test() Method
jsFiddle for those how want to prove something
Remove the global flag. Currently it's searching at the end of your input string for the second time, where it won't find the pattern again. See docs for the lastIndex property.

About the definition of javascript variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
var all = [];
var all = new Array();
What is the difference between these two definitions? I can't make them clear.
There is no difference. But best practice is to avoid using new on JavaScript primitive types.

Categories