Getting value from MathQuill - javascript

I am using MathQuill I would like to get the values of the fraction I got it using Split which wasn't robust !
var str="\frac{12}{3}+\frac{2}{3}";
How to get the values of a fraction using JavaScript ! I found that I should use regrex ! I am new to javascript regrex ! I want values as
var fra1=12;
var fra2=3;
var fra3=2;
var fra4=3;

If you have to use regex, and you only need it for this particular string, then the simplest method is to use .match and convert to number:
const str = "\frac{12}{3}+\frac{2}{3}";
const frasStrings = str.match(/\d+/g);
const frasNumbers = frasStrings.map(Number);
console.log(frasNumbers);

Related

Convert a string to an array without splitting it

Is it somehow possible to convert a string to an array as easy as possible without any hacks? and without splitting the string at some point?
For example bringing this:
temp = 'Text';
to this:
temp = ['Text'];
It might be a simple question but I couldn't find any solution without splitting the string.
const temp = 'text';
console.log(new Array(temp));
console.log([temp]);
console.log(temp.split());
console.log([].concat(temp));
There are a few options out there.
If you want an array with the string as a single value just create a new array with that string.
temp = [temp];

Find and replace a substring after a word in javascript

I have a string which has email id as plain text in it ,I want to replace the email id in the string with hashed value eg.
var str ="Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB"
Before storing the above string I want to hash only testmail5#gmail.com which is after userId=. Hence need suggestions to achieve the same.
Using split twice
var str="Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB"
console.log(str.split('userId=')[1].split('|')[0])
Try this :
str.match(/\userId=(.*?)\|/)[1]
Regex are better to do stuff like that
Quick and dirty way by using URL API in JS.
var data = "Token=wUFvvW4pLDjO2Kh9BkF6ShNXMpWCAH84RQrF2GMSMvkT9ji1HWER/hPcDzQVZ+eqfBnzltOP0+NJTa/x6+XrKcSR090Jka8Awdj13CiSiD5OXwFbCHzYX0nzwkbWJ3m7zvyvjIWJJZ7L53YRHckAeTzA39UWR53/s8PHyL7hUu8=&ssoComplete=true&userId=testmail5#gmail.com|flca+&siteID=OXchfjbB";
var some_hash = "myHashValue";
var data_url = new URL("http://localhost/?"+data); // dirty part
var new_data = data.replace(data_url.searchParams.get("userId").split('|')[0], some_hash);
console.log(new_data);

Split A string on a term AND THEN splitting on another term

I have a poorly designed URL query string that I can't easily change e.g.
https://mysite/.shtml?source=999&promotype=promo&cmpid=abc--dfg--hif-_-1234&cm=qrs-stv-_wyx&aff=45628_THIS+IS+Test_Example
I need to extract elements from it e.g. 45628
At the moment I'm using
document.URL.split(/aff=|_/)[5];
But I don't like this solution because if other parts of the URL structure change which is highly likely then my solution will break
Instead what I want to say is
split on "aff=" AND THEN split on "_"
Is there an easy way to do this, looking for a JS answer
Pretty sure you can do it like this:
document.URL.split("aff=")[1].split("_")[0];
I would start by splitting the string into tokens, if you can. Rather than working with foo=bar&fin=bin, break it down into [['foo', 'bar'], ['fin', 'bin]]. You can do that by splitting on the & and then the splitting each of those on the = character:
const data = 'source=999&promotype=promo&cmpid=abc--dfg--hif-_-1234&cm=qrs-stv-_wyx&aff=45628_THIS+IS+Test_Example';
console.log(data.split('&').map(it => it.split('=')));
Next, take the tokens you want and extract the leading digits:
const data = 'source=999&promotype=promo&cmpid=abc--dfg--hif-_-1234&cm=qrs-stv-_wyx&aff=45628_THIS+IS+Test_Example';
const tokens = data.split('&').map(it => it.split('='));
const [key,val] = tokens.find(([key]) => key === 'aff');
console.log(key, val.match(/[0-9]+/));
var url = 'https://mysite/.shtml?source=999&promotype=promo&cmpid=abc--dfg--hif-_-1234&cm=qrs-stv-_wyx&aff=45628_THIS+IS+Test_Example';
var re = new RegExp(/aff=(\d+)/);
var ext = re.exec(url)[1];
alert(ext)

Using an array of regex expressions for .match

I have something I am trying to accomplish.
I'd like to take an array built with AJAX/xml.
array[/word0/, /word1/, /word2/]
and put this into a form that could be used in a .match():
result = string.match(array)
I have tried using a for loop and stepping through the array using string.match(array[i]) to no avail.
Is there an easy way to do this?
Edit: You may have a syntax problem. The following is not valid syntax:
array[/word0/, /word1/, /word2/]
Something like this fixes it:
var regexps = [/word0/, /word1/, /word2/];
Original answer:
Javascript RegExps already do this. You're looking for:
var regexp = /word0|word1|word2/;
Assuming your list of matches comes back in the right format, you could achieve this like so:
var words = ["word0", "word1", "word2"];
var regexp = new Regexp(words.join("|"));
str.match(regexp);
http://jsfiddle.net/KALPh/
Your approach was fine. Here's my implementation:
var regexes = [/^def/, /^abc/],
testString = 'abcdef',
numRegexes = regexes.length;
for(var x=0;x<numRegexes;x++) {
alert(regexes[x].test(testString));
}
To initialize your array, use
var array = [/word0/, /word1/, /word2/];
Then you can use
str.match(array[i])
If your problem is the transmission in "AJAX/xml", then you'll need to build the regular expressions client side with new RegExp(somestring) where somestring might for example be "word0" : you can't embed a regex literal in XML.

How to substring in jquery

How can I use jquery on the client side to substring "nameGorge" and remove "name" so it outputs just "Gorge"?
var name = "nameGorge"; //output Gorge
No jQuery needed! Just use the substring method:
var gorge = name.substring(4);
Or if the text you want to remove isn't static:
var name = 'nameGorge';
var toRemove = 'name';
var gorge = name.replace(toRemove,'');
Using .split(). (Second version uses .slice() and .join() on the Array.)
var result = name.split('name')[1];
var result = name.split('name').slice( 1 ).join(''); // May be a little safer
Using .replace().
var result = name.replace('name','');
Using .slice() on a String.
var result = name.slice( 4 );
Standard javascript will do that using the following syntax:
string.substring(from, to)
var name = "nameGorge";
var output = name.substring(4);
Read more here: http://www.w3schools.com/jsref/jsref_substring.asp
That's just plain JavaScript: see substring and substr.
You don't need jquery in order to do that.
var placeHolder="name";
var res=name.substr(name.indexOf(placeHolder) + placeHolder.length);
var name = "nameGorge";
name.match(/[A-Z].*/)[0]
Yes you can, although it relies on Javascript's inherent functionality and not the jQuery library.
http://www.w3schools.com/jsref/jsref_substr.asp
The substr function will allow you to extract certain parts of the string.
Now, if you're looking for a specific string or character to use to find what part of the string to extract, you can make use of the indexOf function as well.
http://www.w3schools.com/jsref/jsref_IndexOf.asp
The question is somewhat vague though; even just link text with 'name' will achieve the desired result. What's the criteria for getting your substring, exactly?
How about the following?
<script charset='utf-8' type='text/javascript'>
jQuery(function($) { var a=$; a.noConflict();
//assumming that you are using an input text
// element with the text "nameGorge"
var itext_target = a("input[type='text']:contains('nameGorge')");
//gives the second part of the split which is 'Gorge'
itext_target.html().split("nameGorge")[1];
...
});
</script>

Categories