Regex test, is there a better way to write this? [closed] - javascript

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 7 years ago.
Improve this question
The following code works, but I wonder if there is a better way to achieve the same result:
var regex = /define\((\s+)?['|"](PRIVATE_KEY)['|"],(\s+)?['|"](.*)['|"](\s+)?\)/i;
Test:
regex.test("define('PRIVATE_KEY', 'MYSECRETKEY');");
https://regex101.com/r/pW0qS0/4

First I think you don't need to use pip within character class (if you want to match only one quote and double quote) also instead of (\s+)? you can use \s*:
/define\(\s*['"](PRIVATE_KEY)['"],\s*['"](.*)['"]\s*\)/i
See demo https://regex101.com/r/dL1vF4/3

Related

I Need a regex which can match these two patterns "/enpoint" or "endp/enpoint" [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 2 years ago.
Improve this question
i have a file which has endpoints like this "/enpoint" or "endp/enpoint" it need to match these two patterns and grep the endpoints
thanks in advance
(endp)?\/endpoint
Here is a link to the online regex playground for the above expression.
https://regex101.com/r/I2GuF8/2

Regex matching emails containing * and [.] [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 4 years ago.
Improve this question
I want to write a regex for matching *#xxx[.]gr in js.
My regex so far :
/^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
I want : *#xxx.gr
If I understand your specs correctly this should suit you:
/^[^<>()\[\]\\.,;:\s#"]+#[^<>()\[\]\\.,;:\s#"]{3}\[\.\][^<>()\[\]\\.,;:\s#"]{2}$/g
Demo: https://regex101.com/r/kfjAG9/2

RegEx string - code with 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 5 years ago.
Improve this question
If I have a string in this format:
RED-MPWMC-40486655646-024
How can I extract MPWMC from this string with javascript?
No need of regex here. Just split it using - and access the array element with index=1.
var str = "RED-MPWMC-40486655646-024";
console.log(str.split("-")[1]);

Regular Expression character set only [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 5 years ago.
Improve this question
I tried this : [fl]ady?[ing] ?[rb]ug!?
I know [fl]ad[y]?(ing)? ?[br]ug!? is an answer
Can this be solved using character sets only? Must match both.
ladybug
fading rug!
I believe this should work for you
[fl]ad(y|ing)\s?[br]ug?
Check out http://www.regexpal.com/
Using character sets only? So, it doesn't matter what other letter combinations it matches as well? Mmm. Then
[fl]ad[giny]+[\sbrug!]+
would do. See: https://regex101.com/r/FJWJyM/1

Take all words with replace [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 9 years ago.
Improve this question
I have this code:
$(this).text().replace(/write\s(\w+);/g, "$1");
and this text:
write testing code;
but show only "testing"!
How to take all words and show "testing code"?
thx
console.log($("#my_element").text().replace("write",""));
You don't need a regex if replacing the string "write" is all you want to do.
To change the element's inner html:
$("#my_element").html(($("#my_element").text().replace("write",""));
If I understand the question correctly you want to strip out the word write from the string and keeping the other words? If this is the case, you can do it like this:
$(this).text().replace(/(\s?)write(\s?)/g, '');

Categories