This is my equation
5x^2 + 3x - 5 = 50
I have used this regex
/([+|-])([1-9][a-z])+|([1-9][a-z])+|([+|-])([1-9])+|([1-9])+/mg
but it does not give the results I want.
I want to divide my equation like this
array(
5x^2 ,
3x ,
-5 ,
=50
)
As a starting point, you could split your string by several mathematical operator symbols (for instance +, -, *, / and =). Then you get an array of terms but without the operators that were used to split the string:
const string = "5x^2 + 3x - 5 = 50";
const regex = /\+|\-|\*|\/|=/g;
const result = string.split(regex);
console.info(result);
To retrieve the delimiter characters as well, have a look at this StackOverflow post for example.
First remove the whitespaces.
Then match optional = or - followed by what's not = or - or +
Example snippet:
var str = "5x^2 + 3x - 5 = 50";
let arr = str
.replace(/\s+/g, '')
.match(/[=\-]*[^+=\-]+/g);
console.log(arr);
Related
For example:
let word = 'Winter4000'
const seperate = (word) => {
...
}
seperate(word) // output: Winter 4000
The word can be random and the number is always at the end.
Ian's answer works for most integers, but for decimals or numbers with commas (like 1,000,000), you'll want an expression like
word.split(/([0-9.,]+)/).join(" ");
so it doesn't put an extra space when it runs into a decimal point or comma.
Writing this as a function,
let word = 'Winter4,000.000';
const seperate = (input_word) => {
return input_word.split(/([0-9.,]+)/).join(" ");
}
console.log(seperate(word));
let word = 'Winter4000'
const seperate = word.split(/([0-9]+)/).join(" ")
split it using regex pattern looking for numbers, then join it back together with a space added
const word = 'Winter4000';
const result = word.match(/[^\d]+/) + ' ' + word.match(/[\d]+/);
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 want to extract a number from a string which is a mix of letters and numbers.
For example:
string1 = "1901920100153 abcx"
string2 = "123 1901920100159 abcdf"
string3 = "--> + - asdj 1901920100123 yuman"
now I want to extract only 13 digits number from the above strings which start from 1901920100(10 digits) and last three digits could be change like 1901920100153(13 digit), 1901920100159(13 digit), 1901920100123
I tried Regex expression
string.match(/(\d+)/) but it is extracting 123 from string2 which I doesn't want at all.
Please help finding me the regex expression. Thanks
To match a certain number that starts with a fixed value and then has X amount of digits, you can use a basic pattern like
1901920100\d{3}
If you want to make sure there is no digit on both ends of the number, you can use
(?<!\d)1901920100\d{3}(?!\d)
See the regex demo #1 and demo #2.
Since the second regex contains lookbehind that is still not universally supported by all JS environments, you can re-write it as (?:^|\D)(1901920100\d{3})(?!\d) and grab Group 1 values.
JavaScript demos:
var strings = [ "1901920100153 abcx", "123 1901920100159 abcdf", "--> + - asdj 1901920100123 yuman" ];
var fixed_part = "1901920100";
var regex = new RegExp(fixed_part + "\\d{3}", "g");
for (var i=0; i<strings.length; i++) {
console.log(strings[i], '=>', strings[i].match(regex));
}
ECMAScript 2018+ compliant regex with lookbehind demo:
const strings = [ "1901920100153 abcx", "123 1901920100159 abcdf", "--> + - asdj 1901920100123 yuman" ];
const fixed_part = "1901920100";
const regex = new RegExp(String.raw`(?<!\d)${fixed_part}\d{3}(?!\d)`, "g");
for (var text of strings) {
console.log(text, '=>', text.match(regex));
}
I'm trying to extract the degree rate from the CSS transform property,
transform = "rotate(33.8753deg) translateZ(0px)"
with a regular expression. So far I've succeeded to get almost the exact number:
const re = new RegExp('.*rotate( *(.*?) *deg).*', 'm');
let degRate = transform.match(re);
Output: An array which the third element is:
"(33.8753"
How can I get only the number without the parenthesis?
How can I get only the number? (not in an array)
You can use the RegEx \(([^(]*)deg\) and get the first group with .match(...)[1]
\( matches the first (
([^(]*) captures anything but ( 0 or more times
deg\) matches deg) literally.
let str = "rotate(33.8753deg) translateZ(0px)";
let deg = str.match(/\(([^(]*)deg\)/)[1];
console.log(deg);
Simpler extraction:
let str = "rotate(33.8753deg) translateZ(0px)";
let deg = parseFloat(str.replace(/^.*rotate\(/,""));
console.log(deg);
I have this string:
var s = '/channels/mtb/videos?page=2&per_page=100&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true'
I want to repace per_page number (in this case 100, but it can be any number from 1-100, maybe more?)
I can select first part of the string with:
var s1 = s.substr(0, s.lastIndexOf('per_page=')+9)
which give me:
/channels/mtb/videos?page=2&per_page=
but how would I select next '&' after that so I can replace number occurrence?
dont assume same order of parameters!
You can use following regex to replace the content you want.
regex:- /per_page=[\d]*/g(this is only for your requirement)
var new_no=12; //change 100 to 12
var x='/channels/mtb/videos?page=2&per_page=100&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true';
var y=x.replace(/per_page=[\d]*/g,'per_page='+new_no);
console.log(y);
Explanation:-
/per_page=[\d]*/g
/ ----> is for regex pattern(it inform that from next character onward whatever it encounter will be regex pattern)
per_page= ----> try to find 'per_page=' in string
[\d]* ----> match 0 or more digit (it match until non digit encounter)
/g ---->/ to indicate end of regex pattern and 'g' is for global means find in all string(not only first occurrence)
Use replace with a regular expression to find the numbers after the text per_page=. Like this:
s.replace(/per_page=\d+/,"per_page=" + 33)
Replace the 33 with the number you want.
Result:
"/channels/mtb/videos?page=2&per_page=33&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true"
Start with the index from the lastIndexOf-per_page instead of 0.
Get the index of the first & and create a substr s2 to the end.
Then concat s1 + nr + s2.
I would not use regex, because it is much slower for this simple stuff.
With Array.filter you can do this, where one split the text into key/value pairs, and filter out the one that starts with per_page=.
Stack snippet
var s = '/channels/mtb/videos?page=2&per_page=100&fields=uri%2Cname%2Cdescription%2Cduration%2Cwidth%2Cheight%2Cprivacy%2Cpictures.sizes&sort=date&direction=asc&filter=embeddable&filter_embeddable=true'
var kv_pairs = s.split('&');
var s2 = s.replace((kv_pairs.filter(w => w.startsWith('per_page=')))[0],'per_page=' + 123);
//console.log(s2);
var matches = /(.*\bper_page=)(\d+)(.*)/;
if (matches) {
s = matches[0] + newValue + matches[2];
}