Splitting Pasted Records Without Breaking URLs: Delimiter Detection in JavaScript
Our team pastes multi-field records into a browser tool every day: an ID, a password, a 2FA secret, an email, a cookie string. Depending on who exported them, the fields are glued together with | , ---- , tabs, colons or semicolons. We built a small browser-only formatter that splits these lines, pulls out one column and removes duplicates. These are the parsing decisions that mattered, with code…
In the digital world, multi-field records are often copied into browser tools, containing various pieces of information such as an ID, password, 2FA secret, email address, and cookie string. These fields can be connected using various delimiters like |, ----, tabs, colons, or semicolons, which vary depending on the source of the data. To address this issue, a team of developers created a small browser-only formatter that simplifies the process of splitting lines, extracting specific columns, and removing duplicate entries.
The parsing process is built on several key decisions. First, strong delimiters are prioritized over weak delimiters. Strong delimiters include characters such as ----, |, and tabs, which rarely appear inside a value. If a strong delimiter is present, it is used as the separator. Weak delimiters consist of ;, :, and ,. If a weak delimiter is present, whichever appears first in the line is chosen.
This is achieved using a regular expression with a negative lookahead, ensuring that a colon followed by // is not mistaken for a separator.
Next, the formatter splits the line based on the selected delimiter. The splitLine function accepts a line and an optional separator. If no separator is provided, it uses the detectSeparator function to determine the appropriate delimiter. The split function accepts a regular expression, allowing the negative lookahead to prevent URLs from being split incorrectly. The resulting array is then trimmed for whitespace.
The ID is extracted by attempting to match the most specific shape first. The ID can be found in different contexts, such as the first field, part of a profile URL, or within a cookie string like c_user=. The function looks for the ID in various formats, starting with the most specific shape, which is the most specific pattern. After finding a match, the function proceeds to search for alternative patterns. This approach ensures the most accurate extraction of the ID.
If there are duplicate IDs, they are removed without altering the order of the remaining entries. A Set is used to track seen IDs, while an array maintains the original order. The function iterates through each line and extracts the ID using the previously mentioned extractId function. If an ID is found and hasn't been previously encountered, it is added to both the Set and the output array.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to
More from Dev.to on urgent.news
Report a problem with this story
Get the day's news by email. Daily, weekly or monthly. Sign up