Is it possible to make a regex that accepts ANY substring [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm somewhat new to regex. I understand most of the basics but what I'm trying to do is beyond my knowledge, and may not even be possible.
I'm trying to make a regex in JavaScript that can match a series of function calls in the following pattern.
Name.Name(Params).Name(Params)
The names could be any standard java function name. I understand how do to this part. The params though can be different number of parameters (Currently only 0-2)
My biggest issue however is that params could potentially take ANY string with either a single or double quotation mark, or variable names. I have added some examples below as I need all of these to work with my regular expression (if Possible).
Examples:
Func.Foo().Bar()
Foo.Bar('foo', bar).Foobar()
Foo.Bar("foo", "bar").bar(')')
Foo.Bar('/"foo/"').bar("foo(bar/")")
My main concern here is I cant just look for a opening and parentheses or even 2 quotation marks.
Is it possible to use a regex so that I can parse the function call and parameters out?

The short answer to the Question in the title is yes, you can build a regex that matches any substring. But unfortunately that is not what you want. If you allow arbitrary substrings your regex will either match many cases you dont want to match or it will become extremely complex (see the email regex for an example).
What you want is a tokenizer!(https://medium.freecodecamp.org/how-to-build-a-math-expression-tokenizer-using-javascript-3638d4e5fbe9)
Edit: for the solutions in the comments: the ast parser is for java, the author wants to use javascript.

Related

how to extract an id from a javascript in jmeter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
i have a problem with extracting an id from a request in javascript. Let me explain, although I have inserted as a post processor a regular expression extractor with the following regular expression: (? <= Store_id :) \ d, the extraction of the value does not happen. Strangely, however, when I use this expression in the view result tree search this seems to work, the search finds me exactly the value I need, could you tell me where am I wrong?
You need to wrap the value you want as a group with parenthesis:
store_id:(\d+)
Regular expressions are case-sensitive by default in JS. Try this:
(?<=store_id:)\d+
I replaced \d with \d+ because probably the ID may have more than one digit. If that's not the case, you can use \d.
Try something like:
{store_id\s*:\s*(\d+)
where:
\s* - optional number of whitespaces
\d+ - any number of digits
() - grouping
See Regular Expressions user manual chapter for more details
If you have problems when it comes to coming up with a proper regular expression alternatively you can go for the Boundary Extractor which basically extracts everything between left and right boundaries, it consumes less resources and works faster. More information: The Boundary Extractor vs. the Regular Expression Extractor in JMeter

Is there a best practice to define long strings in JavaScript considering column width? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I sometimes run into situations where I would need a best practice to define a long string. I'm talking about something like this:
const text = 'This is indeed a very long string. Some might say that it is really, really long.'
My problem here is that the string is just too wide. If I would prefer a solution where the column width is considered. I would usually use one of two solutions:
a.)
const text = 'This is indeed a very long string. ' +
'Some might say that it is really, really long.'
The problem with this one is that it uses an unnecessary concatenation.
b.)
const text = `This is indeed a very long string.
Some might say that it is really, really long.`
And the problem with this one is that the resulting string will actually contain a new line, which might not be wanted in some situations.
I realize that this might be a question for opinionated answers, but I still feel like that I'm missing something, or that there is a better solution out there. Please show me if you have one!
You can use the string continuation character \ (single backslash) to do that.
const longString = "This is a really really \
long string that should \
not be split in multiple lines."
console.log(longString)
See the documentation in the Long literal strings section for details.

How does toLowerCase() work in Javascript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I understand that using that function will make an entire string all lowercase. However, I'm curious in the behind the scenes work. I can't find an explanation anywhere on how it works. Does it basically loop through every index in the string and check to see what the character is and if there is a lower case character available?
In general the best place to look for such information is the ECMAScript specification:
The following steps are taken:
Call CheckObjectCoercible passing the this value as its argument.
Let S be the result of calling ToString, giving it the this value as its argument.
Let L be a String where each character of L is either the Unicode lowercase equivalent of the corresponding character of S or the actual corresponding character of S if no Unicode lowercase equivalent exists.
Return L.
For the purposes of this operation, the 16-bit code units of the Strings are treated as code points in the Unicode Basic Multilingual Plane. Surrogate code points are directly transferred from S to L without any mapping.
The result must be derived according to the case mappings in the Unicode character database (this explicitly includes not only the UnicodeData.txt file, but also the SpecialCasings.txt file that accompanies it in Unicode 2.1.8 and later).
Step 3 is the part you're really interested in. As you can see, the details of how "L" is produced are up to the implementation. If you're interested in going deeper the next place to look would be e.g. the V8 engine itself.

What dose it mean 'regex' on javascript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I found this too code in one example script:
'regex':'^[a-zA-Z]{2} *\\d{6}|[a-zA-Z]{2} *\\d{3} *[a-zA-Z]{2}|[a-zA-Z]{2}[a-zA-Z]{2}[0-9]{6}|[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{5}|[a-zA-Z]{2}[a-zA-Z]{1}[0-9]{5}|[a-zA-Z]{2}[0-9]{1}[a-zA-Z]{1}[0-9]{4}|[a-zA-Z]{2}[0-9]{5}[a-zA-Z]{1}|[a-zA-Z]{2}[0-9]{2}[a-zA-Z]{1}[0-9]{3}$',
but i really do not know what it means...
Regex is a regular expression for example disecting parts of text from a bigger collection of text.
Say for example that you want to find all names in a newspaper. Instead of reading the entire thing looking for each name you can make a regex model of finding every word starting with a capital letter that is not right after a punctuation.
In your example the regex ^ means it looks for something that starts with the following:
- A small or capital letter between a to z
- The word is to letters long
Read a couple of examples and you'll get the hang of it.
http://www.dreambank.net/regex.html
It is simply just a model how text is built up.
If you just want the explanation of the posted regex, you should try regex101 . It breaks down the posted regex into capture groups and gives a pretty detailed explanation and matches for a given input and regex.
Like many have already suggested, it would be better if you start by reading about how regex works. I'm sure you'll find plenty of related questions on Stackoverflow.
However, I have created a simple demo of your posted regex on regex101 like I said. I'm going to refrain from posting the entire explanation here. it would be a good exercise if you try to read the explanation and understand it on your own.

Javascript http regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am currently using the following to convert [url=][/url] to a html link:
s = message.replace(/\[url=([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='$1'>$2</a>")
That work's fine.
I then added on another replace function using a regex to replace www with http://www like so:
s = message.replace(/\[url=([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='$1'>$2</a>")
.replace(/www/g, "http://www");
This is probably not the best/efficient method and also does not support https:// which is not a priority at the moment but is something I would like to include at some point. Could someone please advise me on what I could do to improve the regex?
Couple of things:
First, there are some problems with your second pattern. It is not case insensitive (whereas the first pattern is) so it won't catch things like 'WWW'. Maybe that's desirable, maybe not. But it's also global, and is not anchored to the beginning of the URL. So it will replace www anywhere in the URL. Changing it to something like /href=\'www/ and then changing the replace string to href='http://www should solve these problems.
Secondly, in a case like this, using 2 regular expressions may not be bad. You can fold it into one regex if you want, but that doesn't mean it is any more efficient for the computer or the poor human who happens to be reading it.
All that said, one way to accomplish this with a single regular expression, is to do something like:
s = message.replace(/\[url=(?:http:\/\/)?([^\]]+)\]\s*(.*?)\s*\[\/url\]/gi, "<a href='http://$1'>$2</a>")
This may accomplish what you want. It does not key off of "www", though, it simply prepends "http://" to any URL that does not already begin with that string. It also doesn't support https, as you mentioned, but I'm not sure how you will support that anyway. If all you are given is a URL with no protocol, how do you determine whether or not to make it https?

Categories

Resources