? Incorrect regex expression [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 2 years ago.
Improve this question
I’ve been trying to do find if a variable contains a digit inside of it. And for some reason, I put (exp.) ‘Majorpassword09’ and it just prints a false?
// vars
var regex = new RegExp(‘.*\d.*’)
var str = ‘Scorpio08’
// main
function reg1() {
if (regex.test(str)){
console.log(‘true’);
}else{
console.log(‘false’);
}
}
reg1();
EDIT: I’ve put the title as Incorrect regex expression because that's the only thing that I think is wrong.

You're most likely on a mac.
Your code shows that you're using fancy quotes. Like these ones: ‘’. You need to use normal quotes, like ', and ".
Here's a solution to your problem.
// vars
var regex = new RegExp(".*\\d.*")
var str = "Scorpio08"
// main
function reg1() {
if (regex.test(str))
{
console.log("true");
}else{
console.log("false");
}
}
reg1();
Whatever text editor you're using, it has fancy quotes on. Consider looking in the settings, or on Google, to find out how to disable them.

as you are using string to create a RegExp Object thus have to use two backslashes to properly escape
var regex = new RegExp(‘.*\\d.*’)
or
var regex = /.*\d.*/

Related

Problem adding Js variables in HTML with getElementById [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 4 years ago.
Improve this question
<body>
<h1>Operadores aritméticos</h1>
<h3>+ Adición (102+103)=</h3><p id="suma1"></p>
<h3>- Substracción</h3><p id="resta1"></p>
<h3>* Multiplicación</h3><p id="multi1"></p>
<h3>/ División</h3><p id="div1"></p>
<h3>% Módulo</h3><p id="mod1"></p>
<script>
var suma = (102+103);
var resta = (36-20);
var multiplicación = (27*30);
var división = (900/30);
var módulo = (106%3);
document.getElementById("suma1").innerHTML = suma;
document.getElementById("resta1")innerHTML = resta;
document.getElementById("multi1")innerHTML = multiplicación;
document.getElementById("div1")innerHTML = división;
document.getElementById("mod1")innerHTML = módulo;
</script>
Hello guys, I have a problem, im pretty new at this (programming with HTML, Js, etc.).The issue is that when I try to make my Js variables appear on HTML (with document.getElementById), they do not appear. Nevertheless, if erase every document.getElementById except the one containing "suma1", the browser displays me the result of the sum (205), but if I add even one of them, the browser doesn´t display anything.
I hope I was clear with my problem, it seems very simple but hard to explain.
Any suggestions?
Thanks in advance
The reason you're not seeing anything when you add the statements to populate resta1, multi1, div1, and mod1 is probably because they all have a syntax error. This is likely causing even the first statement (suma1, which is syntactically valid) not to work.
Valid Statement
The 1 statement that is working is document.getElementById("suma1").innerHTML = suma;
Invalid statements
All the other statements follow this pattern:
document.getElementById("id")innerHtml = variable;
Note that you're missing the . between getElementById("id") and innerHtml. If you add the missing . then it should all work as expected.

How to Change or replace a part of URL string 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 4 years ago.
Improve this question
I have one url which contains special character like
#1# or #2#
http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#
I want to change with some other value like in place of #1# I need "hello" and In place of #2# I need "there", I tried with some code, but it's not working
var str = response.data;
var mapObj = {
#1#:dagId,
#2#:state
};
str = str.replace(/#1#|#2#/gi, function(matched){
return mapObj[matched];
});
In response.data I am getting same URL which I have mention above,
I am getting:
SyntaxError: Invalid or unexpected token for line no 3.
Is something is wrong in syntax?
I am fine with any syntax angularjs or javascript.
You have to change the regex a bit by wrapping strings with parenthesis. Your object is not valid as well:
var str = 'http://10.10.10.10:8000/admin/taskinstance/?flt1_dag_id_equals=#1#&flt2_state_equals=#2#';
var mapObj = {
"#1#":"dagId",
"#2#":"state"
};
str = str.replace(/(#1#)|(#2#)/gi, function(matched){
return mapObj[matched];
});
console.log(str)

How should this querystring value be formatted in ajax input [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 5 years ago.
Improve this question
I am modifying a code example that I will use (and not very familiar with Javascript)
This is the piece of code
function chk() { cnt++;
var resp=ajax('chk.php', 'POST', 'ordernr='XXXXXX'&r='+((new Date()).getTime()));
if (resp=='3') {
I am posting to this file withe a Querystring-varaible named ordenr
What is the correct syntax to enter the value of Querystring.ordernr instead of XXXXXX
var orderNumber = 1;
var requestParamsStr = 'ordernr=\'' + orderNumber + '\'&r=' + ((new Date()).getTime());
console.log(requestParamsStr);
function chk() {
//cnt++;
var resp = ajax('chk.php', 'POST', encodeURIComponent(requestParamsStr));
//rest of code
}
I think I understand your question. If you need to put quotes around the order number then you must use the escape character / the way I'm doing in the code snippet, so that it doesn't delimit the string (or, alternatively, you could use double quotes within the single quotes). You must also use + to concatenate the the strings.
UPDATE:
I've encoded the request param string as per #ADyson's comment.

If/Else only works for first if [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'm working with this, but the issue is that only the first if ever functions correctly. If I change the order, each if statement works on its own, but the logic where if the first is false, then check the second, and so on isn't working. What am I missing here?
$("#search_button").click(function(){
var table = $('#main_index').DataTable();
var search_term = $("#second_select2 option:selected").text();
var first_s = $("#first_select2 option:selected").text();
if (first_s = 'District'){
table.columns(1).search(search_term).draw();
}
else if (first_s = 'Territory'){
table.columns(2).search(search_term).draw();
}
else if (first_s = 'Region'){
table.columns(0).search(search_term).draw();
}
else {
console.log('error');
}
});
If I console.log the search_term and first_s variables, I can see them changing and correctly working. And, as I said, without the if/else statements each of these works on its own.
In all your comparisons, you need to replace = by ===
You are using the assignment operator (=) in your comparisons. you should be using the identity operator: ===.
if (first_s = 'District'){
needs to be
if (first_s === 'District'){
as do the rest of the checks.

Get the value of a div [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Helllo, I'm having a div like this:
<div>1+1</div>
How can I get these '1+1' to the javascript and calculate them to '2'?
Thanks in advance!
you can use innerHTML property of element by javascript to extract the value:
document.getElementById("div").innerHTML
and then use eval() method to evaluate the math expression:
var exp=document.getElementById("div").innerHTML;
alert(eval(exp));
You need to somehow identify this div, e.g
<div id="div-with-expression">1+1</div>
And code will be like:
console.log(eval(document.getElementById('div-with-expression').innerText));
You can use eval:
var equation = getElementById(IdOfDiv).innerHTML;
var result = eval(equation);
Well, You could use the Javascript eval() func:
http://www.w3schools.com/jsref/jsref_eval.asp
But, eval functions can introduce malicious code into your environment.
I'm not sure as to what your purpose to start with and what your assumptions are for that input but here's another way to go about it with regex although it's more complicated:
var myRegexp = /(\d+)(\+)(\d+)/;
var match = myRegexp.exec("1+1");
// parseInt(match[0]) == 1 ,as an Int
// match[1] = '+', you could figure what operand this is with 'if else' conditions
// parseInt(match[2]) == 1 ,as an Int

Categories

Resources