How to create Regular Expressions from input - javascript

Hi I'm stuck at assignment.
I want to create a regex based on a string sent to function.
If I send for example dd/mm/yyyy, I want it to create (\d\d)[\/](\d\d)[\/](\d\d\d\d) and if yyyy/mm/dd I need it in reverse so I can later use it for date validation?
Is this even possible?

How do you use a variable in a regular expression?
Javascript Regex: How to put a variable inside a regular expression?
I think that's what you are trying to do ? Get user input and use it with RegEx ?
You can use .split() to get an array out of string, and then work with that array.
You can use .reverse() and .join() if you need it later.

Related

Capture value surounded by symbol in string

Inside my strings there is values surounded by $:
"$FORMAT$ is invalid, please check $FILE$ file"
I want to take variable names between $ (for example $FORMAT$ should be - FORMAT) to compare and if they match some condition replace variable in string with some value (for example ".txt"). I tried to achive that functionality with regex:
/\$(\w+)\$/gi
But it takes $ with variable names. How can I modify my regex to make it work properly? Thanks!
/\$(.+?)\$/gi should work well in your case.

RegEx - how to get substring before and behind a certain char in JavaScript?

For example, I have this string: "request:FLTMODE6"
and i want to remove ":FLTMODE6" to get "request" and remove "request:" to get "FLTMODE6"
Im working with NodeJs, how could this be done?
How about using split instead of regex like "request:FLTMODE6".split(':')

JavaScript Regular Expression to get words in quotes string

Thanks for all, I met a problem in JavaScript, and the problem is:
I have a string: such as:
{"results":[{"id":"id1","text":"text1"},{"id":"id2","text":"text2"},{"id":"id3","text":"text3"}]}
Then, I want to get the string such as:
id1|text1|id2|text2|id3|text3
So how should I write the Regular Expression?
Thank you so much, and I am a new comer in StackOverFlow!
I have a string
That string is in JSON format, which works very well with JavaScript.
So how should I write the Regular Expression?
You should not write a regular expression at all. If you did, you only had two problems:
.replace(/(^.+?|"\},\{)?"id":"|","text":"|"}[^"]*$/g, "|").slice(1,-1)
Instead, parse the string into an object, and extract the id-text pairs from there:
var result = JSON.parse(string).results.map(function(el) {
return el.id+"|"+el.text;
}).join("|");

Getting this regex expression to work in javascript

I have an html checkbox element with the following name:
type_config[selected_licenses][CC BY-NC-ND 3.0]
I would like to break this name apart as follows and returned as part of an array:
["type_config", "[selected_licenses]", "[CC BY-NC-ND 3.0]", "[selected_licenses][CC BY-NC-ND 3.0]"]
I thought I could do this by using a regular expression in javascript. Here is the expression that I am using:
matches = /([a-zA-Z0-9_]*)((\[[a-zA-Z0-9_\.\s]*\])+)*/.exec(element_name);
but this is the result I am getting in my matches variable:
["type_config[selected_licenses]", "type_config", "[selected_licenses]", "[selected_licenses]", index: 0, input: "type_config[selected_licenses][CC BY-ND 3.0]"]
I am half way there. What am I doing wrong in my regular expression? I guess I should also ask if it is possible to accomplish what I want with a regex?
Thanks.
The problem with this kind of goal is that there's no simple way to achieve this with regular expression, i.e. a simple match call. In short, even if you put a quantifier after a capturing group, the captured string will always be just one.
You'll have to rely on something more specific, like breaking the string with a repeated use of indexOf, or something like
name.split(/(?=\[)/);
Maybe you want to be sure that name is formally correct.
This is a very ugly problem. I don't know how repeatable this is, but I can do it:
Regex
^(\w+)(?<firstbracket>\[(?<secondbracket>[^]]*)\]\[(.*?)\])$
Replacement
["$1", "[$3]", "[$4]", "$2"]
Demo
http://regex101.com/r/eD9mH8

Translate PHP preg_replace to javascript and set it recursive

Hi i have this simple code:
preg_replace('/[\r\n]+/', '</br>',$string);
i would like to replicate the same codein JS so i do:
string.replace('/[\r\n]+/','</br>');
but this won't work.
Also i would like to make it recursive using g , is it possible?
Thanks
replace() accepts a first parameter of type RegExp and you are passing a string, you need to change to:
string.replace(/[\r\n]+/,'</br>');
Also, replace() isn't a static method, in other words you don't call it like this. Instead you call it on the string on which you want to perform the replacement.
If you want to use g then you can:
string.replace(/[\r\n]+/g,'</br>');
^
In JavaScript, .replace supports both strings and regexes as what to replace. You sent it a string, it doesn't know it's a regex. It was looking for the literal string '/[\r\n]+/'.
You need to use a regex literal:
string.replace(/[\r\n]+/g,'</br>');
Yes, you can use use regexes like that, with no quotes!
You can use the new RegExp constructor if you want to use a string as your regex:
string.replace(new RegExp('/[\r\n]+/', 'g'),'</br>');

Categories

Resources