Required solution for a problem:
Id is fixed ‘qualityassurance’ I want to generate ids like: 1- q.ualityassurance
For second run id should be like: 2- qu.alityassurance
For third run it should be like: 3- qua.lityassurance
and so on till id’s length. But i dont understand how to recall function for next run with incremented value.
Code is:
Selenium.prototype.doNothing = function(){
// The do in front of Nothing in the function is what tells the system this is a global function
}
Selenium.prototype.doRandomEmail = function(locator, num)
{
var id = “qualityassurancetask”;
var stringLength = id.length;
var randomstring = ”;
var insstring = ‘.’;
var num = 1;
//var rnum = Math.floor(Math.random() * stringLength);
var remainingstring = id.substring(num,id.length);
randomstring += id.substring(0,num).concat(insstring, remainingstring);
randomstring += “#gmail.com”
num= num+1;
selenium.doType(locator,randomstring,num);
}
Try something like this:
next_address = last_address.replace(/\.(.)/, "$1.");
The idea is to look for ".?" where ? is any character (note that the literal . in the regex is used to match any character, hence my use of .? to explain). This is changed to "?.", i.e. the dot is "pushed" rightward in the string. If the new and old addresses are identical, then you've run out of combinations.
Note if your address strings contain the #gmail.com part, you need to avoid moving the dot past the #, which you can do like this:
next_address = last_address.replace(/\.(\w)/, "$1.");
That is, only allow swapping the dot with "word" characters.
Related
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!
I am capturing the user`s session data into a variable and come out as the following:
"192605238.|1=Identifiant=FSAPPS\BBF4U5C=1^2=Caisse=GTD95600=1^3=Editeur=False=1^5=Pvp=PVPTSP=1"
I am trying to figure out how to capture the data after FSAPPS\ and before the = so in this case I only want to output BBF4U5C. This would be the login name of the user and may vary in lenght.
Looking for any help at this point.
Try this:
var s = "192605238.|1=Identifiant=FSAPPS\BBF4U5C=1^2=Caisse=GTD95600=1^3=Editeur=False=1^5=Pvp=PVPTSP=1"
var res = s.split("FSAPPS")[1].split("=")[0];
Here is a hackish way based on finding the start and end indexes in the string. I'm not sure what parts are reliable and what parts aren't but this will work for your specific use case.
var str = "192605238.|1=Identifiant=FSAPPS\BBF4U5C=1^2=Caisse=GTD95600=1^3=Editeur=False=1^5=Pvp=PVPTSP=1";
var start = str.indexOf("FSAPPS") + 6;
var end = str.indexOf("=1^2");
alert(str.substring(start, end));//alerts BBF4U5C
http://jsfiddle.net/7ssyub7a/
Find index of identifier (index of first letter),
find index od "=" (start looking after identifier)
with this information + known length of identifier you can easily extract sub string containing your login, (bonus points for not destroying your original string)
var txt =
"192605238.|1=Identifiant=FSAPPS\BBF4U5C=1^2=Caisse=GTD95600=1^3=Editeur=False=1^5=Pvp=PVPTSP=1";
var start = txt.indexOf("FSAPPS");
var end = txt.indexOf("=", start);
var login = txt.substring(start + 6, end);
I have a calculation which works fine until it hits a thousand separator, which is a comma. I have tried a few things trying to get rid of the comma but I can't seem to get it right. Below is the last thing I tried which was a total failure. The 'tot_amount' is the one I need the comma stripped from. I think I went down the wrong track and it should be doable without the extra 'tot' variable. Please assist.
function updateDue() {
var total = parseInt(document.getElementById("tot_amount").value);
var val2 = parseInt(document.getElementById("eftposamount").value);
var tot = total.replace(",","");
// to make sure that they are numbers
if (!tot) { tot = 0; }
if (!val2) { val2 = 0; }
var ansD = document.getElementById("remainingval");
ansD.value = tot - val2;
}
Replacing
Your code is only removing one comma, try using this instead:
total.replace(/,/g, "");
The best way is to remove all non-number safe stuff and use that:
+total.replace(/[^\de.-]/gi, "")
As you see I've a g
The g stands for global meaning it will replace all commas instead of just the first one.
String to Integers
Instead of:
var total = parseInt(document.getElementById("tot_amount").value);
var tot = total.replace(",","");
You must make it an integer after you parse it. A short way to parse a number is using +
var total = +document.getElementById("tot_amount").value.replace(/[^\de.-]/gi, "")
Your code:
You can make your function this:
function updateDue() {
document.getElementById("remainingval").value =
+document.getElementById("tot_amount").value.replace(/[^\de.-]/gi, "") -
+document.getElementById("eftposamount").value.replace(/[^\de.-]/gi, "")
|| 0
}
The ||0 will remove the need for the ifs.
Calling parseInt on a comma-delimited string will give you unexpected results:
parseInt('1,000'); // => 1
Instead, you want to remove commas, and then parse the integer:
var i = +('1,000'.replace(/,/g, '')); // => 1000
The // part is simply RegExp notation, and the g afterwards is a flag that tells the parser to look for global instances of the expression. In your case, you want to remove all commas, so the g flag is appropriate.
For the sake of saving 7 characters in your code, you can simply coerce your value to an integer with + rather than calling parseInt.
Let's say I have a string like this:
var test = my.long.file.name.zip
I am getting the total number of periods in this string with javascript like so:
var dots = (test.match(/\./g) || []).length;
I would then like to replace all of the periods in the string with underscores if there is more than one period in the string.
if(dots>"1"){
var newname = test.replace(/\./g, "_");
console.log(newname);
}
The problem is that this is replacing all of the periods. I would like to keep the last on intact. So what I would like the newname variable to read as would be:
my_long_file_name.zip
My guess is that I should use $.each() somehow to iterate over all except the last one to change the name. How should I do this?
You dont necessarily need a loop, you could do it with a more complex regex, which uses a positive lookahead
The regex /\.(?=.*\.)/g finds periods, but only where there is a subsequent period somewhere further along, which means the last one is not matched.
window.onload = function(){
var input = "my.long.file.name.zip"
var result = input.replace(/\.(?=.*\.)/g,'_')
alert(result);
}
Consider splitting the string on '.', then re-joining all but the last with '_':
var test = "my.long.file.name.zip";
parts = test.split('.');
var plen = parts.length;
if (plen > 1) {
test = parts.slice(0, plen - 1).join('_') +
"." +
parts[plen - 1];
}
console.log(test);
a lookahead group in regex will work:
var test = 'my.long.file.name.zip';
var result = test.replace(/\.(?=[^.]*\.)/g, '_');
alert(result);
this matches a dot followed by ('anything but dot' and another dot), replacing only what is outside the group
var test = 'my.long.file.name.zip';
var last_index = test.lastIndexOf('.');
var newname = test;
if (-1 !== last_index) {
newname = test.replace(/\./g, '_');
newname = newname.substring(0, last_index).concat('.', newname.substring(last_index + 1));
}
console.log(newname);
I have two strings of equal number of slashes and the same letter in each position. There is always one letter and a square bracket indicating an index. There will always be a corresponding letter in both strings.
var first = "/a/b[1]/c/d[3]/e[1]/f"
var second = "/a/b[1]/c/d[4]/e/f"
I expect output should be
result = "/a/b[1]/c/d/e/f"
This is what I came up with but maybe there's a better way of doing it as it returns /a/b/c/d/e/f which is not what i wanted. http://jsfiddle.net/3PM9H/
var first = "/a/b[1]/c/d[3]/e[1]/f".split("/");
var second = "/a/b[1]/c/d[4]/e/f".split("/");
for (var i=0; i < first.length; i++){
firstMatch = first[i].match(/\[([0-9]+)\]/g);
secondMatch = second[i].match(/\[([0-9]+)\]/g);
if (firstMatch != secondMatch){
first[i] = first[i].replace(/\[([0-9]+)\]/g,'') //get rid of the square bracket.
}
}
first.join("/");
I just solved this here. It's a node project but the main file is dependency-less hence portable.
Use the dynamic programming algorithm to compute the Levenshtein distance, and use that transform to generate the regex. Subsitutes become wildcards, inserts or deletes become optional characters.
var Levenshtein = require('levenshtein-transformation)';
var lev = new Levenshtein(str1, str2);
console.log(lev.regex());