I am trying to extract the integers from a string.
String :
Str = "(Start = 10) AND (End_ = 40)"
Note: The integers here can range from 1 - 999, single digit to three digits
Desired Output:
No1 = 10
No2 = 40
This code will get you what you want, an array of numbers found in the string.
Explanation
The regular expression looks for a single number 1 through 9 [1-9] followed by 0, 1, or 2 {0,2} numbers between 0 through 9 [0-9]. The g means global, which instructs match() to check the entire string and not stop at the first match.
Code
var str = "(Start = 10) AND (End_ = 40)";
var numbers = str.match(/[1-9][0-9]{0,2}/g);
console.log(numbers);
Related
I have a string formed by number and mathematical operator like "1 + 1 *1" that is the text content of the number appendend on the screen div, I want to form an array of them and then divide it using mathematical operators such as + or - as a divisor, the problem is that when I try to divide them the array is actually divided, except for when the "-" sign is present, in fact if I have as a string "1 + 1 * 1 -1" the result will be an array ["1", "1", "1-1"] while it should be ["1", "1", "1", "1"]
Thanks everyone in advance.
let regex = /[+ | - | * | / ]/
let Arrays
Arrays = screen.textContent.split(regex);
You seem to be confusing alternatives with character sets.
Put the operators inside a character set, and optional spaces around it.
You need to escape - because it's used to separate the ends of a character range (unless you put it at the beginning or end of the character set).
let regex = /\s*[+\-*/]\s*/;
let text = '1 + 1 * 1 -1';
console.log(text.split(regex));
UPDATE
Splitting the string at +, -, *, /
let screentextContent = "1 + 1 * 1 -1"
let regex = /[+\-*/]/
let Arrays
Arrays = screentextContent.split(regex);
console.log(Arrays)
white space after 1 or before 1 will be preserved.
const str = "1 + 1 * 1 - 1";
let regex = /[+|\-|*|/]/
let arr
arr = str.split(regex).map(itm => itm.trim());
console.log(arr);
I am working on a Chat bot for discord that has an addition calculator.... I am currently trying to find the .indexOf the first time a number appears in the string... For ex: !add 1 + 1 would be the command to add 1 and 1... I have an array that I use that contains all single numbers ex:
const singleNumbers = [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
];
when I get the string back I am using
for (const num of singleNumbers){
const num1 = msg.content.indexOf(num,);
const add = '+';
const locationOfAdd = msg.content.indexOf(add,);
const num2 = msg.content.indexOf(num,locationOfAdd);
const add1 = msg.content.slice(num1,) * 1;
const add2 = msg.content.slice(num2,) * 1;
msg.reply(add1 + add2);
}
When I run this... It for some reason will only use the first number of the Array so the numbers I use in !add 1 + 1 have to start with 0... so !add 01 + 01 which in math is fine... but for simplicity how do I make it be able to start with any number in the array rather than the first... If you don't know discord.js,
msg.content
Is the string returned so if I type in chat...
Hey Guys what's goin on?
it would return as a String ("Hey Guys what's goin on?")...
To sum this all up... I am wondering how to make my const num that I declared in my for of loop check for all the numbers in its array rather than just the first in my case 0.
If you just want to find the index for the first digit str.search(/[0-9]/) or str.match/[0-9]/).index.
Using regex to extract the numbers, and reduce to add them:
match on: string starts with !add, at least one space, at least one digit as a capture group, optional space and a + sign, another series of digits as a capture group.
You get an array with [full match, digit group 1, digit group 2]. I slice off the full match, and then use reduce to (+x converts the string to a number; you could also use Number(x)) to add each number and collect it in sum.
const sum = msg.content.match(/^!add\s+(\d+)\s*\+\s*(\d+)/).slice(1).reduce((sum,x)=>+x+sum,0)
Note: \d is the same as [0-9]
JavaScript.info RegExp tutorial
Below is regex code for getting the number 6 from my tesetstr. How can i extract the string 'months' from teststr using regex ?
var teststr = '6 months';
var num = /(\d+)\s*month/;
var days = teststr.match(num)[1];
console.log(days);
Currently, (\d+) matches one or more digits capturing them into Group 1 (note you are not using the group at all, so, it is redundant).
You seem to want to only match digits before a space + "month". Use the following regex:
var num = /(\d+)\s*month/;
and then access the captured value with
var days = ($('#infra_time_threshold').text()).match(num)[1] * 30;
^^^
Alternatively, you could use a lookahead right after \d+:
var num = /\d+(?=\s*month)/;
and then just use your .match(num)[0] since Group 0 value will be the whole match.
NOTE: You might want to add a null check before accessing the 0th or 1st index of the match object.
I have a string like
5|10|20|200|300
and i want to get the First Digit Before | and last digit after | that is 5 and 300.
How would I use regex in javascript to return that numbers??
This simplest regex will return the two matches 5 and 300:
^\d+|\d+$
See the matches in the demo.
In JS:
result = yourString.match(/^\d+|\d+$/g);
Explanation
^\d+ matches the beginning of the string and some digits (the 5)
OR |
\d+$ matches some digits and the end of the string
JavaScript only keeps the last capture for (...)+, so you can write
var m = "5|10|20|200|300".match(/(\d+)(\|(\d+))+/);
Then m[1] is "5" and m[3] is "300"
var string = '5|10|20|200|300';
var array = string.split('|');
//array[0] = '5';
//array[array.length-1] = '300';
It's not regex I know, but I've always found split easier to work with in most cases.
Convert the string into an array using split() method
var str="5|10|20|200|300";
var res_array = str.split("|");
now to get first and last value of an array:::
alert("First value is"+ res_array[0]);
alert("Last value is"+ res_array[arr.length - 1]);
var number = '731231';
var myRegex = /[0-6]/;
console.log(myRegex.test(number));
Can anyone explain this ?
IMO the regex written as [0-6] will only check for numbers between 0 and 6 but in the above case the value as large as 731231 is also evaluated to be true
Your regex matches, when there is any such digit present. If you want to match only such digits, use
/^[0-6]+$/
This matches a string with any number of digits from 0-6. If you want a single digit, omit the +:
/^[0-6]$/
You're checking if there is somewhere a number between 0 and 6, which is the case.
var number = '731231';
var myRegex = /^[0-6]+$/;
console.log(myRegex.test(number));
UPDATE
Though, if you want the string to match a number satisfying the rule 0 >= N <= 6
This would be what you want
var myRegex = /^[0-6]$/;
Yiu should use
var myRegex = /^[0-6]$/;
you're looking for a string that starts ^ then contains one 0..6 digit and immediately ends $
Your implementation /[0-6]/ is looking for a 0..6 digit anywhere within the string, and that's why 731231 meets this criterium.
Your regex is checking to see if one of the following appears in the string:
0, 1, 2, 3, 4, 5 or 6
and that is true because there is 1, 2 and 3 in there.
If you want to check that the whole string is a number then you could use:
var myRegex = /^[0-9]+$/;