Javascript regex to find a particular string which could be broken across multiple lines - javascript

I'm trying to write a regex which gets a particular substring from a string. However, the substring could be broken across multiple lines.
I've tried using the multiline flag, like this:
"foo\nbar".match(/foobar/m)
But that returns null.
I've also seen a number of posts suggesting I use [\S\s]. However, as far as I can tell, this only works if you know where the break line will be, like this:
'foo\nbar'.match(/foo[\S\s]bar/m)
Is there a way to find all instaces of foobar in a string when the line break could anywhere in the string?

Is there a way to find all instances of foobar in a string when the line break could anywhere in the string?
Remove all line-breaks from subject before comparing with your regex.
See this simple demo:
const arr = ["foo\nbar", "\nfoobar", "fo\nobar", "foobar\n", "foobar"];
const val = 'foobar';
arr.forEach(function(el) {
console.log(el.replace(/\n/, '') == val)
});

Related

Find and replace complex term in a string using Regex

I Need to wrap any expression in a string (it can be multiline string with many expressions, mixed with regular words) that starts with abc.(efg|xyz).bar. with curly braces.
I'm using find and replace approach using the following Regex:
const MY_REGEX = /"?(?<![a-zA-Z0-9-_])((?:abc\.(efg|xyz\.bar))(?:\.[a-zA-Z0-9-_]*)+)"?/gm
someInput.replace(MY_REGEX, '{{$1}}')
My strategy works fine for simple cases like this:
const input = 'abc.efg.bar.name.first, abc.xyz.role, non.captured.term'
// outputs: {{abc.efg.bar.name.first}}, {{abc.xyz.role}}, non.captured.term
But fails miserably for a complex inputs like this one:
const input = 'abc.xyz.bar.$func(foos[param[name="primary" and bool=true]].param[name="new"].multiValue)'
// outputs: {{abc.xyz.bar.}}$func(foos[param[name="primary" and bool=true]].param[name="new"].multiValue)
// Should be: {{abc.xyz.bar.$func(foos[param[name="primary" and bool=true]].param[name="new"].multiValue)}}
I'm looking for a more robust way or better regex to do it.
Any suggestions?
Based on the examples you gave, it looks like , should be the delimiter between expressions. So (?:\.[a-zA-Z0-9-_]*)+) to [^,]* to match everything until the next ,.
const MY_REGEX = /"?(?<![a-zA-Z0-9-_])((?:abc\.(efg|xyz\.bar))[^,]*)"?/gm;
console.log('abc.xyz.bar.$func(foos[param[name="primary" and bool=true]].param[name="new"].multiValue)'.replace(MY_REGEX, '{{$1}}'));
console.log('abc.efg.bar.name.first, abc.xyz.role, non.captured.term'.replace(MY_REGEX, '{{$1}}'));

Use only one of the characters in regular expression javascript

I guess that should be smth very easy, but I'm stuck with that for at least 2 hours and I think it's better to ask the question here.
So, I've got a reg expression /&t=(\d*)$/g and it works fine while it is not ?t instead of &t in url. I've tried different combinations like /\?|&t=(\d*)$/g ; /\?t=(\d*)$|/&t=(\d*)$/g ; /(&|\?)t=(\d*)$/g and various others. But haven't got the expected result which is /\?t=(\d*)$/g or /&t=(\d*)$/g url part (whatever is placed to input).
Thx for response. I think need to put some details here. I'm actually working on this peace of code
var formValue = $.trim($("#v").val());
var formValueTime = /&t=(\d*)$/g.exec(formValue);
if (formValueTime && formValueTime.length > 1) {
formValueTime = parseInt(formValueTime[1], 10);
formValue = formValue.replace(/&t=\d*$/g, "");
}
and I want to get the t value whether reference passed with &t or ?t in references like youtu.be/hTWKbfoikeg?t=82 or similar one youtu.be/hTWKbfoikeg&t=82
To replace, you may use
var formValue = "some?some=more&t=1234"; // $.trim($("#v").val());
var formValueTime;
formValue = formValue.replace(/[&?]t=(\d*)$/g, function($0,$1) {
formValueTime = parseInt($1,10);
return '';
});
console.log(formValueTime, formValue);
To grab the value, you may use
/[?&]t=(\d*)$/g.exec(formValue);
Pattern details
[?&] - a character class matching ? or &
t= - t= substring
(\d*) - Group 1 matching zero or more digits
$ - end of string
/\?t=(\d*)|\&t=(\d*)$/g
you inverted the escape character for the second RegEx.
http://regexr.com/3gcnu
I want to thank you all guys for trying to help. Special thanks to #Wiktor Stribiżew who gave the closest answer.
Now the piece of code I needed looks exactly like this:
/[?&]t=(\d*)$/g.exec(formValue);
So that's the [?&] part that solved the problem.
I use array later, so /\?t=(\d*)|\&t=(\d*)$/g doesn't help because I get an array like [t&=50,,50] when reference is & type and the correct answer [t?=50,50] when reference is ? type just because of the order of statements in RegExp.
Now, if you're looking for a piece of RegExp that picks either character in one place while the rest of RegExp remains the same you may use smth like this [?&] for the example where wanted characters are ? and &.

Splitting string with javascript using '>' character

I acknowledge that this question has probably been asked so many times before and I have tried searching all over StackOverflow for a solution, but so far nothing has worked for me.
I want to split a string but it's not working properly and spitting out individual characters as each item in an array. The string I have from my CMS uses ">" characters to separate and I am using regEx to replace the 'greater than' symbol - with a comma, which works. Sourced this solution from Regex that detects greater than ">" and less than "<" in a string
However, the arrays remain incorrectly formed, like the split() function does not even work:
var myString = "TEST Public Libraries Connect > News Blog > A new item"
var regEx = /<|>/g;
var myNewString = (myString.replace(regEx,","))
alert(myNewString);
myNewString.split(",");
alert(myNewString[0]);
alert(myNewString[1]);
alert(myNewString[2]);
I've put it up in a Fiddle as well, just confused as to why the split won't work properly. Is it because there is spaces in the string?
This should work:
var myNewString = myString.split(">");
https://jsfiddle.net/2j56cva0/3/
In your fiddle, you were splitting myNewString instead of the actual string.
myNewString.split(",");
You need to assign the result of the split to something. It does not just change the string itself into an array.
var parts = myNewString.split(",");

split string of chain functions with reg-exp

I looked around on StackOverflow but could not find the answer!
Anyway, I was wondering how do I get following in JavaScript with regexp.
I have a string
bla().func1($(ele).attr("data")).func2.func3("test")... ;
and I want extract func1, func2, func3... from the given string, anybody have an example of how to do that?
Bear in mind, that the function names changes, so they are not func1, func2 ... they can be load, past, moon ... whatever
try this:
var str = 'bla().func1($(ele).attr("data")).func2("test")';
alert(str.split(/(?:\.)\w+\d+(?=\()/g));
There's the fiddle:
http://jsfiddle.net/cv6avhfb/3/
This code would split the string in parts while using as separators substrings like '.func1(', '.func2(', '.abc3(' and other. If the function names have different structure you just have to change the \w+\d+ part of the regex.
Here's the result of this code:
bla(),($(ele).attr("data")),("test")
And if you like to know more about regex in javascript:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Repeatedly strip out innermost parenthesized expressions:
var str = 'bla().func1($(ele).attr("data")).func2.func3("test")... ';
var expression = /\([^(]*?\)/g;
while (expression.test(str)) { str = str.replace(expression, ''); }
document.write(str.split('.').slice(1).join(' '));
However, you shouldn't do this (why do you want to?). JS should be parsed by, well, JS parsers. For instance, the above will fail miserably if there's a string literal containing a parenthesis. You should parse this using Esprima and analyze the resulting parse tree.

Splitting string in javascript

How can I split the following string?
var str = "test":"abc","test1":"hello,hi","test2":"hello,hi,there";
If I use str.split(",") then I won't be able to get strings which contain commas.
Whats the best way to split the above string?
I assume it's actually:
var str = '"test":"abc","test1":"hello,hi","test2":"hello,hi,there"';
because otherwise it wouldn't even be valid JavaScript.
If I had a string like this I would parse it as an incomplete JSON which it seems to be:
var obj = JSON.parse('{'+str+'}');
and then use is as a plain object:
alert(obj.test1); // says: hello,hi
See DEMO
Update 1: Looking at other answers I wonder whether it's only me who sees it as invalid JavaScript?
Update 2: Also, is it only me who sees it as a JSON without curly braces?
Though not clear with your input. Here is what I can suggest.
str.split('","');
and then append the double quotes to each string
str.split('","'); Difficult to say given the formatting
if Zed is right though you can do this (assuming the opening and closing {)
str = eval(str);
var test = str.test; // Returns abc
var test1 = str.test1; // returns hello,hi
//etc
That's a general problem in all languages: if the items you need contain the delimiter, it gets complicated.
The simplest way would be to make sure the delimiter is unique. If you can't do that, you will probably have to iterate over the quoted Strings manually, something like this:
var arr = [];
var result = text.match(/"([^"]*"/g);
for (i in result) {
arr.push(i);
}
Iterate once over the string and replace commas(,) following a (") and followed by a (") with a (%) or something not likely to find in your little strings. Then split by (%) or whatever you chose.

Categories

Resources