Javascript - Update to pipe delimiter with array that contains commas - javascript

I've been looking through stack overflow and the internet in general for hours with no solution in site yet...
I'll explain what I'm attempting to do - I'm building a list, then using php explode command by comma so I can loop through it, and insert each record into the database.
However, I'm building the array javascript, and the values I'm building the array off of, contain commas, separating first name and last name (Format Ex: Firstname, Lastname).
I'm a beginner and I'm still learning, so any help to point in the right direction would be greatly appreciated. I'm wondering if I need to do math to count the commas on the php side in order to separate based off the comma count, or if this is achievable in javascript using regex..
Everything I've been looking at seems like I may have to learn some regex to resolve this... Perhaps there's a built in jQuery command to change delimiter that I haven't been able to find?
var labels = $('.list-left ul li.active').map(function(){
return $(this).text().split(',').join('|');
}).get();

Maybe like this:
var test='test1,test2,test3,test4';
var test_new=test.replace(new RegExp(',','g'), '|');
console.log(test_new);

Related

Google App Script - Docs - Removing unused Items After Merging Content - RegEx?

I have a Google Form bound to a Sheet. My Script is in the sheet. Sometimes a set of fields in the Form will be blank / not filled in, this is intentional.
When this happens I want to remove these unused merging elements from the Doc before saving and closing it. There can be 7 to 20 unused elements that I want to remove when this happens.
I have tried so many things. I do not want to hard code it as there will be changes made/ other additions made moving forward. This would be an easy fix but not a correct solution.
I am thinking the RegEx after the merge is the way to go, but I'm open to any suggestions.
The Fields are formatted like this: {{NAME}} or {{OtherName}} or {{Some Name}} or {{Street Name}}, {{State Name}} as in an address. I am also trying to remove any extra spaces and am struggling as to how to remove the ",". Maybe I should be adding the "," when merging the field instead of it being a static part of the page.
Here is my RegEx. I'm new to it and tried following the GAS guide to build this:
body.replaceText(/([{]{2}|^)[\w.]\s{0,1}[\w.]{0,1}([}]{2}|$)/ , '');
//OR
body.replaceText(/(\W\W|^)[\w.]\s{0,1}[\w.]{0,1}(\W\W|$)/ , '');
//OR
var pattern = /([{]{2}|^)[\w.]\s{0,1}[\w.]{0,1}([}]{2}$)/;
body.replaceText(pattern , '');
I have spent hours on this issue trying Start and End, loops and any other solution I could think of or find to adapt to my need and am at my wits end.
Thanks

Regex on multiple lines of random order

I am using BIRT to design a report based on a database, and one of the fields of the form contain multiple lines, like that :
Site: Place ThePlace
Room: D2 RMD3
InstanceId: OI-RandomChars
The fact is, they are not always in this order, as it is user input (in an other form, not BIRT prompt).
And please note that these fields contain capital letters.
So what I want to do is to extract in three separate columns, so with three regex in JavaScript the Site, the Room, and the InstanceId.
I have tried many things with like catching each row until the end of the line or playing around substrings with various conditions ... and so far I think the best to do is to use string function replace to remove anything different than what I want to catch.
An example for the row Room would be :
row["Log"].replace(/?![Room:\s\S*\s]/, "")
I get an error with this but you can see what I try to do.
Thanks for all the consideration about my problem.
A single regex can become overly complicated and hard to read and mantain for this kind of job.
I would probably consider doing it programmatically like your first instinct was.
First I would consider splitting the string into lines
var lines = string.split("\n\n");
This will split all your double lines into an array
["Site: Place ThePlace", "Room: D2 RMD3", "InstanceId: OI-RandomChars"]
Then cycle trough all your lines and then make your checks.
Your check now can actually be regex if you want, or using substring.
This is an example:
var site, room, InstanceId;
var siteCheckRegex = new Regex("^Site:");
for(var i=0; i<lines.length; i++){
if(siteCheckRegex.test(lines[i])){
site = lines[i].replace("Site:","");
}
[...]
}
It actually depends on what you really want to get out of it and the problem and difference you can find in the user input data.

Intro to JS student using isNaN (in combination to other statements) to extract string

I suppose I will start out with baby steps...
I am a student that is trying to figure out an assignment. My instructor leaves a lot of reading between the lines on assignments. Currently we are doing string properties. Previously we were doing If/Else, while/loop, for & function statements.
For this assignment we are to prompt for a hypothetical filename (i.e. Spring2014_CS412_1.pdf) and parse it, slice it into the appropriate variables and make it alert a multitude of specifics, such as Semester:Spring, Year:2014, Class:CS412.
Problem is, this instructor doesn't seem to understand the concept of explaining the individual building blocks and how they should be added and nested together. He just throws it all in a bucket and we have to figure the pieces and how they show interact.
That said...I was hoping someone would be willing to take me under their wing and help me walk through this? I don't want the answer outright given to me, but rather help me figure out the overall structure and then help correct any granular mistakes I may overlook.
The assignment is due before Thanksgiving, so I am in a small state of panic since I have already burned several hours on this!
We are to use combinations of the following --
if/else
while loop
for loop
function
arrays
.length
.charAt
.trim
.indexOf
.substring
.toUpperCase
.toLowerCase
.lastIndexOf
.replace
isNaN
Thanks!!
Use prompt to get the String object (http://www.w3schools.com/jsref/met_win_prompt.asp)
String.split() it on "_" (http://www.w3schools.com/jsref/jsref_split.asp), for example:
var myString = "hello_world";
myString.split("_"); // This will return the array: ["hello", "world"]
I don't see where isNaN() is necessary; is there more to the assignment?

Error with a BigQuery regular expression

This question builds off a previous question that I asked:
How do I remove the first character of a string and treat the remaining values as an integer in BigQuery
I am having trouble getting a regular expression that I need for some client work to function. Basically, I want to look through all the cells in a column which has the following types of entries:
customer-o400744190
o400748216
o455239157-new-customer
other similar types with o4552334214 somewhere in the cell
and use something like REGEX_EXTRACT() to parse out or extract "oXXXXXXXXX" from every cell & dump those values into a new column. The data in the column I am pulling from is stored in a string, and can stay that way. Does anyone have any suggestions?
I worked around the problem by just using:
RIGHT(hits_transaction_transactionId, 10)
but know that I am only getting some of the cases that apply. Thus, this is not an acceptable long term solution. Any ideas are greatly appreciated.
Depending on how your data universe is, you could go with /o[0-9]+/
so you would be extracting any ocurrences of o and then at least one number

jQuery tablesorter - sorting a column with mixed text and numbers

I have a table with a column of data that is mixed text and numbers. I'm sorting it using jQuery and the tablesorter plugin. The data that won't sort correctly is equipment tags, for example, "AHU-1", "AHU-2", "AHU-10". The problem is, given those example values, AHU-10 will be placed between AHU-1 and AHU-2. I've found forcing a 'digit' sort doesn't solve the problem.
Here's my question: 1) Does anyone know of an existing parser that I can use in this situation? If there isn't one then I'll need to write my own parser, in which case 2) How should I write the parser? Should I try and translate every letter to a number and do a numeric sort? That's my initial thought.
One more thing, I don't know for sure that a hyphen will be the delimiter. "AHU-1" could also be "AHU1", or "AHU 1", or "AHU:1", or something else.
You do need to write your own parser. What you are looking for is called "natural sort". There are plenty of javascript natural sort algorithms out there. I couldn't find one prewritten for the tablesorter plugin, but googling turns up quite a few.
Assuming all you need to do is sort any string of the pattern AAA-1 as AAA-01 you could do the following:
var myTextExtraction = function(node)
{
// extract data from markup and return it
return node.childNodes[0].childNodes[0].innerHTML
.replace(/([A-Z]{3}-)(\d)/,'$1-0$2');
}
$(document).ready(function()
{
$("#myTable").tableSorter( {textExtraction: myTextExtraction} );
}
);

Categories