JavaScript match() function does not give output [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
In JavaScript code I use regular expression match() function to find pattern and show the result, but it does not give a proper answer, means finding [i] character in giving sentences
<button onclick = "searchPattern()">pattern</button><br>
<p id = "demo"></p>
<script>
function searchPattern(){
var str = "Visti W3Schools!";
var paat = /[i]/g;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>

Read your JavaScript console
Uncaught ReferenceError: patt is not defined
You changed your variable name from paat to patt half way through your code.

Related

jQuery How To Use Backticks In Append [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I wanted to interpolate variables in strings in JS so I used ``(backticks) as shown here -
How To Interpolate Variables In String in JS
Then, I Wanted To put IF-Statements in jQuery Append So I got this -
IF Statements In jQuery Append
But When I use Both Together , Backticks Don't Output Text As Usual-
$("main").append(`Hello ${my_var}`+(second_var>1?"hi ":"bye")+`Bye ${my_var})`
This Results Only In "hi" , The Backticks Before And After The Ternary Operator Don't Output Anything.
HELP ??
You can do something like the below.
const my_var = "Name";
const seconde_var = 2;
console.log(`Hello ${my_var} ${seconde_var >1 ? "hi": "bye"} bye ${my_var}`);

why does array.length give "Uncaught TypeError: array.length is not a function" error in Javascript? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to get the last element of an array using Javascript which is the last "word" in the text of an HTML Tag.
<select name="resultsSelectionBox" id="resultsSelectionBox" onchange="myFunction()"></select>
<script>
function myFunction() {
var selectedValue = document.getElementById("resultsSelectionBox").value;
document.getElementById("demo").innerHTML = "You selected: " + selectedValue;
var separatedSelectedValue = selectedValue.split(" ");
var lastItem = separatedSelectedValue[separatedSelectedValue.length() - 1]
console.log(separatedSelectedValue);
}
The variable separatedSelectedValue is an array created by splitting the contents of selectedValue. When I try to run the code, there is an error underlined for the line "separatedSelectedValue.length()" which has this message "Method expression is not of Function type".
And if I try to run the code, I get this error "Uncaught TypeError: separatedSelectedValue.length is not a function" I thought .length was a function of Arrays in JS, and if not how do I get the length of an array?
The problem is separatedSelectedValue.length(), which means to use length as a function, but it's a property, so you will need .length without the () to get its value.
Use this
var lastItem = separatedSelectedValue[separatedSelectedValue.length - 1]
More information

Javascript: Regex for "DD/MM/YY HH:SS" not succeeding [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following datetime string: 09/07/2020 02:00
I need to check the format of the string before apply Date.parse() to ensure that it's in the correct format. I've developed a regular expression for the string however it doesn't appear to work in Javascript. I've tested it and it works in regex tester but not in js. Example (regex101) here
REGEX
String
09/07/2020 02:00
Expression
^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$
function clean_inputdatetime(datetime) {
var cleaned_datetime = null
var datetime_regex_cond = RegExp('/^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$/', 'g')
var regex_success = datetime_regex_cond.test(datetime)
if (regex_success) {
var cleaned_datetime = Date.parse(datetime)
}
return [regex_success, cleaned_datetime]
}
console.log(
clean_inputdatetime("09/07/2020 02:00")
)
Note regex_success returns false for above.
When using RegExp you need to remove the enclosing slashes - otherwise they will be included in the regex itself. Also you need to correctly escape the string:
const datetime_regex_cond = new RegExp('^([1-9]|([012][0-9])|(3[01]))(\\/)([0]{0,1}[1-9]|1[012])(\\/)\\d\\d\\d\\d (20|21|22|23|[0-1]?\\d):[0-5]?\\d$','g')
Or simply do:
const datetime_regex_cond = /^([1-9]|([012][0-9])|(3[01]))(\/)([0]{0,1}[1-9]|1[012])(\/)\d\d\d\d (20|21|22|23|[0-1]?\d):[0-5]?\d$/g;

Return the first "n" count of characters of a string ( Javascript ) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I've used this :
String.Prototype.left = function left(count){
return this.substr(0,count);
}
And apply it this way :
var string = "Hello Stack!";
console.log(string.left(5));
And the console tells me :
TypeError: example.left is not a function
console.log(example.left(5));
How can I fix it? And where is the problem?
String.prototype.left = function(index){
return this.substring(0,index);
}
var str = "Hello World";
console.log(str.left(5));

document.getElementById() not getting a value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a div:
<div id="results"></div>
and in my js:
document.getElementById('results').innerHTML = "foo";
this works correctly however...
I try to store it...
var rslt = document.getElementById('results');
so I can use it more easily. However "rslt" is undefined and in firebug when I mouse over "results" inside the getElementById brackets it doesn't display any info. Like it's a string??
I'm sure this is probably very simple and I just can't see it...
When I call rslt it gives "null" now. But if I remove the "var reslt = " the rest of it "document.getElementById('results')" works perfectly and returns the div.
window.onload = function(){
var rslt = document.getElementById('results');
rslt.innerHTML = "This is working.";
};
Well, if it is undefined that means one simple thing - the object hasn't made it to the DOM tree yet.
Make that call after you're sure the div has been written to the document, e.g. after load event.
It is Working Perfectly..Look at the Fiddle
<div id="results"></div>
document.getElementById('results').innerHTML = "foo";

Categories