How to find number inside [] Javascript [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following syntax.
var name = [Name_is][234]
var number = find [234];
How can i find this number in javascript/jquery which is inside [] ?

Use a regular expression.
var string = "[Name_is][234]"
var matches = string.match(/\[(\d+)]/);
if (matches.length) {
var num = matches[1];
}
Check out a working fiddle: http://jsfiddle.net/rEJ5V/, and read more on the String.match() method.

if you want to extract the text between the [ ], you can do:
var name = "[Name_is][234]";
var check= "\{.*?\}";
if (name.search(check)==-1) { //if match failed
alert("nothing found between brackets");
} else {
var number = name.search(check);
alert(number);
}

Related

How can filter string that don't match in totally? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I want to filter string that not have all coincidences (I deleted blank spaces)
With string.includes()
'videocardgigabytegeforce3070'.includes('videocardgigabyte') return true
'videocardgigabytegeforce3070'.includes('videocardgeforce') return false
I want to second case also return true, If you have a solution with function or regex I'll appreciate it
const str = 'videocardgigabytegeforce3070';
const regex = /videocard.*geforce/;
const result = regex.test(str);
console.log(result); // true
or
const str = 'videocardgigabytegeforce3070';
const regex = /videocard.*geforce/;
const result = str.match(regex);
if (result && result.length > 0) {
console.log(true); // true
} else {
console.log(false);
}

Invalid expression term '[' using c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Can someone assist me what's wrong in my code. I'm try to convert JavaScript code to C# code
public class SplitString
{
public static string[] Solutions(string str)
{
arr = [];
for(var i = 0; i < str.Length; i += 2){
second = str[i+1] || '_';
arr.push(str[i] + second);
}
return arr;
}
}
And i encounter this error
src/Solution.cs(5,11): error CS1525: Invalid expression term '['
src/Solution.cs(5,12): error CS0443: Syntax error; value expected
javascript and C# are completely different languages; .NET / C# arrays are fixed size - so, you might want a list here:
var arr = new List<string>();
for(var i = 0; i < str.Length; i += 2){
var second = str[i+1]; // || '_'; <== this on the right makes no sense in C#
arr.Add(str[i] + second);
}
return arr.ToArray();

How can I replace the text between two delimiters with a blank? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a message, received via an input field. For example:
This is *red* colour.
I want to replace everything between the two asterisks with a blank line ("___") so the outcome would be:
This is ___ colour.
How can I achieve this?
function myFunction() {
var str = "This is *red* color";
var startIndex = nthIndex(str,'*',1);
var endIndex = nthIndex(str,'*',2);
var output = str.replace(str.substring(startIndex, (endIndex+1)), "_");
}
function nthIndex(str, pat, n){
var L= str.length, i= -1;
while(n-- && i++<L){
i= str.indexOf(pat, i);
if (i < 0) break;
}
return i;
}

regular expression to insert * at proper places in an equation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have an expression such as xsin(x) it is valid only if * comes between x and sin(x) and make it as x*sin(x)
my idea is first search for x then insert * between x and another variable if there is a variable.
equation
sin(x)cos(x) to sin(x)*cos(x)
pow((x),(2))sin(x)to pow((x),(2))*sin(x)
sin(x)cos(x)tan(x) to sin(x)*cos(x)*tan(x)
etc
I am trying with this code..
function cal(str)
{
//var string = "3*x+56";
var regex = /([a-z]+)\(\(([a-z]+)\),\(([0-9]+)\)\)\(([a-z0-9\*\+]+)\)([\*\-%\/+]*)/;
var replacement = "$1($2($4),($3))$5";
while(str.match(regex))
{
str = str.replace(regex,replacement);
}
return str;
}
This one matches right parentheses followed by a letter (e.g. )s ), and inserts a * (e.g. )*s )
It also replaces x followed by a letter with x* and that letter
It should work for x+sin(x) and xsin(x)
function addStars(str) {
return str.replace(/(\))([A-Za-z])/g,function(str, gr1, gr2) { return gr1 + "*" + gr2 }).replace(/x([A-Za-wy-z])/g,function(str, gr1) { return "x*" + gr1 })
}
document.write(addStars("x+sin(x)tan(x)ln(x)+xsin(x)"))
Help from:
JavaScript - string regex backreferences
qwertymk's answer
> 'sin(x)cos(x)'.replace(/(?!^)\w{3}/g, '*$&')
< "sin(x)*cos(x)"
> 'pow((x),(2))sin(x)'.replace(/(?!^)\w{3}/g, '*$&')
< "pow((x),(2))*sin(x)"
> 'sin(x)cos(x)tan(x)'.replace(/(?!^)\w{3}/g, '*$&')
< "sin(x)*cos(x)*tan(x)"
This says: replace anything that doesn't start at the beginning and has three letters with a * and everything that matched

How to restore sentence in javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have this
var input = "([lazy({(jumps{fox([quick(The)]brown)})over}the)]dog)";
I want to get
The quick brown fox jumps over the lazy dog.
Any ideas? I tried to use RegEx but can not find.
We need to have stack kind of approach with the array here. See the below implementation.
var res = "([lazy({(jumps{fox([quick(The)]brown)})over}the)]dog)".split("");
var txt = [],lvl=-1;
res.forEach(function(e,i){
if(e=='('||e=='{'||e=='['){
lvl++;
} else if(e==')'||e=='}'||e==']'){
lvl--;
} else {
if(typeof txt[lvl]=='undefined'){
txt[lvl] = e;
} else {
txt[lvl] = txt[lvl] + e;
}
}
});
txt = txt.reverse().join(" ");
console.log(txt);
if(lvl!=-1) {
//this will alert if any missing parenthesis
alert("Pattern error in input");
}
Edit : updated as per Question owner's description of the pattern in input.

Categories