I'm quite new to the javascript world an have no idea about regex; I hope you can help me with that one:
I need a function that gives me the elements of a text-block that a user can input through an <input/ on a website, so i can output them to another <input/.
Generalized input:
txt1/txt2_txt3#txt4_txt5#txt6
Real input-example ("personalcode"):
user/855042,5_512125#2431072,25_729106#coursname optionaladdition
What I got so far is the html stuff and this (-yep thats not much):
var base= document.getElementsByName("personalcode")[0].value;
What I would need to get out is:
var one = txt1; //always letters
var two = txt2; //always a decimal number
var three = txt3; //always a decimal number
var four = txt4; //always a decimal number
var five = txt5; //always a decimal number
var six = txt6; //can be letters and decimal numbers
There will never be special characters such as !"§$%&/()=?+*# inside a text element. ö, ü, ä is possible.
Example:
var one = user;
var two = 855042,5;
var three = 512125;
var four = 2431072,25;
var five = 729106;
var six = coursname optionaladdition;
In the end I want to output it like this:
document.getElementsByName("output-user")[0].value= one;
.
.
.
I hope you understand what I mean.
var str = "user/855042,5_512125#2431072,25_729106#coursname optionaladdition";
var arr = str.split(/\/([\d,]+)_([\d,]+)#([\d,]+)_([\d,]+)#/);
# => ["user", "855042,5", "512125", "2431072,25", "729106", "coursname optionaladdition"]
I hope i understand you right what you want to achieve.
I made a small fiddle for you how to get your Data.
https://jsfiddle.net/zasg4zgx/6/
Here is the Code:
<form>
Login :
<input id="logthis" type="text" name="fnName" value="user/855042,5_512125#2431072,25_729106#coursname Löcher in Socken flicken">
<input type="button" value="Login" onClick="javascript:SeperateLoginString(logthis.value)">
</form>
With the id i can transfer the Value of the login field to the function.
function SeperateLoginString(logData) {
var seperateString = [];
var temp = new String(logData);
temp = temp.replace(/#/g, ' ').replace(/_/g, ' ').replace(/#/g, ' ').replace(/\//g, ' ');
seperateString = temp.split(" ");
var user = seperateString[0];
var value1 = seperateString[1];
var value2 = seperateString[2];
var value3 = seperateString[3];
var value4 = seperateString[4];
var value5 = seperateString[5];
With this loop you can add the "optionaladdition" to your value.
I managed it so it can have more than one value
for (var i = 6; i < seperateString.length; i++) {
value5 += " " + seperateString[i];
}
alert(value5);
}
Regards,Miriam
Since you are asking for six different variables, I suggest you use six different input tags. This would be easier for the user and especially for you as a developer. Parsing strings like this is asking for trouble.
However, you could get the values from the string using regex. For example, if you want your first variable (letters only), you could do something like this:
var 1 = text.match(/([A-z])+\//g).slice(0, - 1);
It basically matches a group of characters that starts with letters and ends with a forward slash. The slice method removes the last character from the string (the forward slash).
The second var could be selected like this:
var 2 = text.match(/([0-9])+\#/g).slice(0, - 1);
Still, I recommend you to just use multiple inputs. It's way cleaner and less prone to errors. Good luck!
Related
I have a VERY long string containing code from a Rich Text Editor. I need to split this up into 4 parts to save it to the database. I am doing this.
var fullPart = $('#summernote').summernote('code').replace("'", "\'");
var markupStr = fullPart.substring(0, 3000000);
var markupStr2 = fullPart.substring(3000000, 3000000);
var markupStr3 = fullPart.substring(6000000, 3000000);
var markupStr4 = fullPart.substring(6000000);
markupStr, markupStr3 and markupStr4 all contain values, but markupStr2 is empty. What am I doing wrong?
var markupStr2 = fullPart.substring(3000000, 3000000);
Explanation : Start and End index are same in this that is why you are getting empty results.
Check here for more information.
str.substring(indexStart[, indexEnd])
indexStart The index of the first character to include in the returned
substring.
indexEnd Optional. The index of the first character to exclude from
the returned substring.
This is a simple mistake. fullpart.substring(3000000,3000000) would return a string of length of 3,000,000 - 3,000,000 characters (0 characters). The correct way to do this is:
var fullPart = $('#summernote').summernote('code').replace("'", "\'");
var markupStr = fullPart.substring(0, 3000000);
var markupStr2 = fullPart.substring(3000000, 6000000);
var markupStr3 = fullPart.substring(6000000, 9000000);
var markupStr4 = fullPart.substring(12000000);
I'm doing a tracking application for my company and I really need your help
I've got some strings that display it wrong
I'll get the postcode/zipcode and the city name and the "function" (for example distrubition basis)
The string I get is something like that (it's swiss and the format is postcode cityname function)
place = "5506 MägenwilDistributionsbasis";
now postcode is "5506"
cityname is "Mägenwil"
function is "Distributionsbasis"
my question is how can I split the cityname and function (for this example now)?
is it possible to do it with regex or an if statement?
You can split the string using the following regexp:
var myString = "5506 MägenwilDistributionsbasis";
var units = /(\d+ )([A-Z][^A-Z]+)(.+)/g.exec(myString);
Check out contents of array units: there you see units[0] is the whole string, and units[1], units[2], units[3] are what you need.
Note According to comments I must say, it's just a draft for possible solution to let you understand how to start working on the problem in JS. So when you will test your application with more complicated city names and function names in the "magic string", try to figure out what regexp fits your purposes perfectly, because ([A-Z][^A-Z]+) definitly will not match all the known city names.
You could implement that in the most primitive way. Something like this:
place = "5506 MägenwilDistributionsbasis";
var codeAndNameAndFunction = place.split(" ");
var code = codeAndNameAndFunction[0];
var nameAndFunction = codeAndNameAndFunction[1];
var startOfTheFunction;
for (var i = 1, len = nameAndFunction.length; i < len; i++) {
myCharacter = nameAndFunction.charCodeAt(i);
if (myCharacter >= 65 && myCharacter <= 90) {
startOfTheFunction = i;
break;
}
}
var name = nameAndFunction.slice(0, startOfTheFunction);
var functionName = nameAndFunction.slice(startOfTheFunction,nameAndFunction.length);
This is a slight modification of Florian Peschka's answer:
You can split the string using the following regexp:
var myString = "5506 Yverdon-les-BainsDistributionsbasis";
var units = /(\d+ )(.+)([A-Z][^A-Z]+)/g.exec(myString);
Check out contents of array units: there you see units[0] is the whole string, and units[1], units[2], units[3] are what you need.
Note that this will only work if the "function" name is always in the form of Capital Letter followed by Non-capital letters.
I have a variable in JavaScript that holds the below value:
<label>AAA</label>
I need just the AAA. I try to replace the characters but it is failing. Would someone please suggest the best approach?
var company="<label>AAA</label>";// I am getting this value from element
var rx = new RegExp("((\\$|)(([1-9]\\d{0,2}(\\,\\d{3})*|([1-9]\\d*))(\\.\\d{2})))|(\\<)*(\\>)");
var arr = rx.exec(company);
var arr1 = company.match(rx);
if (arr[1] != null) {
var co = arr[1].replace(",", "");
}
}
As you say you need only AAA, consider the below code.
I have taken a substring between the first '>' character in the string company, added 1 to that and the last < character. However, if the company var contains more of such < or >, you could go for a regex approach.
var company="<label>AAA</label>";
alert(company.substring(company.indexOf('>')+1, company.lastIndexOf('<')));
I have the following string: 0-3-terms and I need to increment the 3 by 20 every time I click a button, also the start value might not always be 3 but I'll use it in this example..
I managed to do this using substring but it was so messy, I'd rather see if it's possible using Regex but I'm not good with Regex. So far I got here, I thought I would use the two hyphens to find the number I need to increment.
var str = '0-3-terms';
var patt = /0-[0-9]+-/;
var match = str.match(patt)[0];
//output ["0-3-"]
How can I increment the number 3 by 20 and insert it back in to the str, so I get:
0-23-terms, 0-43-terms, 0-63-terms etc.
You're doing a replacement. So use .replace.
var str = '0-3-terms';
var patt = /-(\d+)-/;
var result = str.replace(patt,function(_,n) {return "-"+(+n+20)+"-";});
Another option is to use .split instead of regex, if you prefer. That would look like this:
var str = '0-3-terms';
var split = str.split('-');
split[1] = +split[1] + 20;
var result = split.join('-');
alert(result);
I don't understand why you are using regex. Simply store the value and create string when the button is called..
//first value
var value = 3;
var str = '0-3-terms';
//after clicking the button
value = value+20;
str = "0-" + value + "-terms"
Hi all,
I have a string coming from my database:
var theString = "LNDSH - LONDON SHOPS";
I need to get two variables out of it.
One with the code before the -, and the other one with the sentence after the -.
To do so I do:
var sentence = $.trim(theString.substring((theString.indexOf('-')+1),theString.length));
var code = $.trim(theString.substring(0, theString.indexOf('-')));
var sentence is ok, but I cannot get rid of the spaces before the - in the code variable.
I really need to get rid of those spaces.
Please note that in var sentence I'm doing +1 because it is always one space in between the sentence and the -.
But in the case of the code: I don't know the length of the code and I don't know how many spaces will be before the -
I've tried:
code.replaceAll("\\s+", " ");
But this does not show a thing in my page (no javascript errors either).
I'm using jquery-1.5.1.min and jquery-ui-1.8.14.custom.min
Thanks a lot!
You may use split with regular expression:
var values = "LNDSH - LONDON SHOPS".split(/\s*-\s*/);
console.log(values[0]); // "LNDSH"
console.log(values[1]); // "LONDON SHOPS"
Try this:
variable.replace(/\s/g,'');
Edit: The above will not seperate your strings, it will only remove the white spaces.
To seperate the strings you can do this:
var seperate = theString.split("-", 2);
var LNDSH = seperate[0];
var LONDON_SHOPS = seperate[1];
var theString = "LNDSH - LONDON SHOPS";
var vett = theString.split("-");
var a = vett[0].trim();
var b = vett[1].trim();
var test = theString.split("-");
alert(test[0].trim());
alert(test[1].trim());