How to restore sentence in javascript [closed] - javascript

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.

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);
}

How do I check spelling of a user input? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
I'm creating a website where the user types whatever he hears in an audio file. On submit, I want to compare his input to the actual paragraph and then display the misspelled words. How can I do that?
// answerParagraph is the true answer (string)
// inputParagraph is the input from the user (string)
let mistakes = 0;
let mistakedWords = [];
for(let i = 0; i < answerParagraph.length; i++){
if(answerParagraph[i] != inputParagraph[i]){
mistakes++
mistakedWords.push(answerParagraph[i])
}
}
alert("You have " + mistakes + " typos!")
I think this should work. :)
Edit: Well if you want to check every word one by one instead of the whole paragraph then the code is:
// answerParagraph is the true answer (string)
// inputParagraph is the input from the user (string)
let mistakes = 0;
let answerWords = answerParagraph.split(" ")
let inputWords = inputParagraph.split(" ")
for(let i = 0; i < answerWords.length; i++){
if(answerWords[i] != inputWords[i]){
mistakes++
}
}
alert("You have mistakes in " + mistakes + " words!")

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;
}

How to find number inside [] Javascript [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 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);
}

Categories