Translate PHP preg_replace to javascript and set it recursive - javascript

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>');

Related

How to use regexp without built-in regexp in js?

In a weird case of mine, I cannot use the built-in regexp in js when I try to inject js to WebView.
What could be my second best thing to use? I basically use regexp to detect:
line-feed(someString.match(/\n/))
cartridge return(someString.match(/\r/))
split string into words(manyWords.split(/\s+/))
But other ways to achieve regexps without js built-in regexp will be appreciated as well.
Use javascript's built-in string.prototype.indexOf and string.prototype.split, there's really no need for regex for those cases.
For line feed you can use someString.indexOf('\n') > -1
For cartridge return you can use someString.indexOf('\r') > -1
For splitting to words you can use manyWords.split(' ')
Incase you want the split a string that contains line breaks to words, you need to nest the split like this:
manyWords.split('\n').flatMap(function(line) { return line.split(' ');});

How to create Regular Expressions from input

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.

Does typescript/javascript have a #"\" equivalent like C#?

Is there a way that I can put slashes in a string in javascript without having to put two?
In C# I could do this:
#"This\is not used as an escape\"
(The # in front of the string makes it not use \ as an escape char.)
Is there something like this in javascript/typescript?
No, there is not this kind of escaping in javascript
String literals
The # is called a verbatim string in C#. One of the key objectives is to allow you have strings that are multiline.
To get the same effect you use a template string in TypeScript / JavaScript : https://basarat.gitbooks.io/typescript/content/docs/template-strings.html
However you would still need to escape any usages of \. 🌹

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("|");

Javascript Regular Expression to parse JSON

I asked a question about regular expression in PHP (which got answered), I need help with same regular expression in javascript, here is the link to previous question. Regular expression to parse JSON
Again I am not looking to get JSON.parse and get the json object, I need to find the regex for the pattern.
Thanks
/\[\[.*?\]\]/g
G - Global (find more than once)
for complex objects, you can further try this -
\{.*\:\{.*\:.*\}\}
{"ABC":{"Prop1":false,"Prop2":"abc","Prop3":false}}
Tested it # http://www.regextester.com/
Try something like:
var matches = text.match(/\[\[.*?\]\]/);
matches[0] will be the matched string.
Since the DOTALL PCRE option isn't supported in Javascript, you'd have to use a regular expression like that:
var matches = text.match(/\[\[(?:\s|.)*?\]\]/);

Categories

Resources