Regex matching help for split pattern - javascript

I've been banging my head against the wall on building a querystring parser using javascript. Normally you would split on the & but there are some weirdo keys that have & in the key that need to be dealt with.
EG:
a.&activitymap.&page=https%3A%2F%2Fsitdev.online&currency=eur
I wish the key to match and strip out the & from the key if there is any which would give this result:
a.activitymap.page=https%3A%2F%2Fsit54.az.dev.online, currency=eur
I have tried a several times to match these results but no luck:
(\w\.?&?)(?!(=\w+))
I would appreciate any help, thanks

Related

Looking to see how I can split a string into two parts, while keeping full words intact

As the title says, looking to see how I can split a string into two parts, while keeping full words intact, and having access to both parts with JavaScript. I am using this string in a pug file.
For instance, I have a very long string
let str = `hello i am a very long string that needs to be split into two different parts! thank you so much for helping.'
I want the first 70 characters, to use as a headline, then the rest to use for the body of the element.
I have used this regex that I found on an older post that gives me the first 70+ characters, with full words...
str.replace(/^([\s\S]{70}[^\s]*)[\s\S]*/, "$1")
which does what I want it to do. But now I need to figure out if there is a way to get the rest of that string?
str1 = `hello i am a very long string that needs to be split into two different`
str2 = `parts! thank you so much for helping.`
Again, using this in a pug file, so if there is a simple answer, I would love that. Otherwise I suppose I could just write a function and import the script to the pug view.
react code

Why do I need to replace \n with \n?

I have a line of data like this:
1•#00DDDD•deeppink•1•100•true•25•100•Random\nTopics•1,2,3,0•false
in a text file.
Specifically, for my "problem", I am using Random\nTopics as a piece of text data, and I then search for '\n', and split the message up into two lines based on the placement of '\n'.
It is stored in blockObj.msg, and I search for it using blockObj.msg.split('\n'), but I kept getting an array of 1 (no splits). I thought I was doing something fundamentally wrong and spent over an hour troubleshooting, until on a whim, I tried
blockObj.msg = blockObj.msg.replace(/\\n/g, "\n")
and that seemed to solve the problem. Any ideas as to why this is needed? My solution works, but I am clueless as to why, and would like to understand better so I don't need to spend so long searching for an answer as bizarre as this.
I have a similar error when reading "text" from an input text field. If I type a '\n' in the box, the split will not find it, but using a replace works (the replace seems pointless, but apparently isn't...)
obj.msg = document.getElementById('textTextField').value.replace(/\\n/g, "\n")
Sorry if this is jumbled, long time user of reading for solutions, first time posting a question. Thank you for your time and patience!
P.S. If possible... is there a way to do the opposite? Replace a real "\n" with a fake "\n"? (I would like to have my dynamically generated data file to have a "\n" instead of a new line)
It is stored in blockObj.msg, and I search for it using blockObj.msg.split('\n'),
In a JavaScript string literal, \n is an escape sequence representing a new line, so you are splitting the data on new lines.
The data you have doesn't have new lines in it though. It has slash characters followed by n characters. They are data, not escape sequences.
Your call to replace (blockObj.msg = blockObj.msg.replace(/\\n/g, "\n")) works around this by replacing the slashes and ns with new lines.
That's an overcomplicated approach though. You can match the characters you have directly. blockObj.msg.split('\\n')
in your text file
1•#00DDDD•deeppink•1•100•true•25•100•Random\nTopics•1,2,3,0•false
means that there are characters which are \ and n thats how they are stored, but to insert a new line character by replacement, you are then searching for the \ and the n character pair.
obj.msg = document.getElementById('textTextField').value.replace(/\\n/g, "\n")
when you do the replace(/\\n/g, "\n")
you are searching for \\n this is the escaped version of the string, meaing that the replace must find all strings that are \n but to search for that you need to escape it first into \\n
EDIT
/\\n/g is the regex string..... \n is the value... so /\REGEXSTUFFHERE/g the last / is followed by regex flags, so g in /g would be global search
regex resources
test regex online

Match between simple delimiters, but not delimiters themselves

I was looking at JSON data that was just in a text file. I don't want to do anything aside from just use regex to get the values in between quotes. I'm just using this as a way to help practice regex and got to this point that seems like it should be simple, but it turns out it's not (at least to me and a few other people at the office). I've matched complicated urls with ease in regex so I'm not completely new to regex. This just seems like a weird case for me.
I've tried:
/(?:")(.*?)(?:")/
/"(.*?)"/
and several others but these got me the closest.
Basically we can forget that it's JSON and just say I want to match the words value and stuff out of "value" and "stuff". Everything I try includes the quotes, so I'd have to clean the strings afterwards of the delimiters or else the string is literally "value" with the quotes.
Any help would be much appreciated, whether this is simple or complicated, I'd love to know! Thanks
Update: Alright so I think I'll go with (?<=")(.*?)(?=") and read things by line without the global setting on so I just get the first match on each line. In my code I was just plopping in a huge string into a var in the code instead of actually opening a file with ajax/filereader or having a form setup to input data. I think I'll mark this as solved, much appreciated!
You have two choices to solve this problem:
Use capturing groups
You can match the delimiters and use capturing groups to get the text within. In this case your two regexes will work, but you need to use access capturing group 1 to get the results (demo). See How do you access the matched groups in a JavaScript regular expression? for how to do that.
Use zero-width assertions
You can use zero-width assertions to match only the text within, require delimiters around them without actually matching them (demo):
(?<=")(.*?)(?=")
but now since I'm not consuming the quotes it'll find instances between each quote, not just between pairs of quotes: e.g., a"b"c" would find b and c.
As for getting just the first match, I think that'll happen by default in JavaScript. You'd have to ask for repeated matching before you see the subsequent ones. So if you process your file one line at a time, you should get what you want.
get the values in between quotes
One thing to keep in mind is that valid JSON accepts escaped quotes inside the quoted values. Therefore, the RegEx should take this into account when capturing the groups which is done with the “unrolling-the-loop” pattern.
var pattern = /"[^"\\]*(?:\\.[^"\\]*)*"/g;
var data = {
"value": "This is \"stuff\".",
"empty": "",
"null": null,
"number": 50
};
var dataString = JSON.stringify(data);
console.log(dataString);
var matched = dataString.match(pattern);
matched.map(item => console.log(JSON.parse(item)));

JavasScript Regex to exclude specific characters which are stored in a string variable, spaces, and change remaining characters to underscores

This is for a hangman-type guessing game. I already figured out how to use a Regex to display the letters as underscores on the page with appropriate spacing. Now I want use a Regex to do the following, all in one expression:
Check a string containing the correct answer this.answers[arraysIndex], against the string containing all of the user's correct guesses rightString
In the correct answer string: change only the letters that don't match the correct guesses string into underscores. This means I want to keep the spaces unchanged too.
I've tried this:
var regex = new RegExp("/(?![^"+rightString+"])/[\A-Za-z/])/","g");
newDisplay = (this.answers[arraysIndex]).replace(regex, "_");
...and this:
newDisplay = (this.answers[arraysIndex]).replace("/(?![^+rightString+])/[\A-Za-z/])/g", "_")
...and countless slight variations of each. I'm not married to the idea of using a string variable, I could use an array variable too, or maybe there's something that hasn't even occurred to me. I've researched exhaustively on here and many other resources (that's how I solved my first problem) but this one's got me beat. Any help is greatly appreciated.

Regex to match date with string as month

I have the following string:
var cur = "t+20d";
And I want to match it. That part I already did with
if(cur.match(/^[t]\+[0-9]{2,4}[dmyw]/i))
Now I also need to be able to match this string, and prefably in the same regex
var cur = "10may15+20d";
I have tried
cur.match(/^([t]|([0-9]{1,2}(jan|feb|march|apr|may|jun|jul|aug|sept|okt|nov|dec)))\+[0-9]{2,4}[dmyw]/i)
But it doens't work as intended.
if I try to compile the subpart I get two pieces of array instead of one
cur.match(/[0-9]{1,2}(jan|feb|march|apr|may|jun|jul|aug|sept|okt|nov|dec)/i);
//yields ["10MaY", "MaY"]
And this worries me about false positives.
I'm really really rusty at regex, last time I tried to make a complicated regex was 15 years ago and that was in perl, so I could really use some help with this one. I know ors and grouped matches are possible, I just can't figure out how to do it anymore so some help is appriciated.
You need to match the number which exists after the month.
^(t|[0-9]{1,2}(?:jan|feb|march|apr|may|jun|jul|aug|sept|okt|nov|dec)\d+)\+[0-9]{2,4}[dmyw]
DEMO
With the help of #AvinashRaj who pointed me to the group operator ?: with his regex I managed to compose this regexfor my uses and i'm posting it here for future users who might need to match a date string like this. ddmmmyy
cur = "10-apr-1115+20d";
cur.match(/^(?:t|[0-9]{1,2}(?:jan|feb|mar|apr|may|jun|jul|aug|sep|okt|nov|dec|[-\/](?:[0-9]{1,2}|jan|feb|mar|apr|may|jun|jul|aug|sep|okt|nov|dec)[-\/])(?:[0-9]{2}|[0-9]{4}))\+[0-9]{1,4}[dmyw]/igm);

Categories

Resources