JavaScript Regular Expression to get words in quotes string - javascript

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

Related

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.

Replacing New Line Character

I need a little help replacing a special character in a string using a regular expression that I just can't seem to figure out.
Here is the string that I have, and the special character is a "↵". Essentially I'd like to replace every "↵" with a comma.
var string = "http://espn.com↵http://yellowpages.com↵http://reddit.com↵http://usps.com http://uber.com↵http://cnn.com↵http://w3schools.com↵http://hitch.com↵http://sdsu.com↵http://sf.com↵http://kings.com"
A little background about what I'm doing, perhaps there is an easier way to do this. I'm getting the value of a textarea where each entry (url) is typed out on a new line and trying to format it into an array of urls.
The string above is what I'm getting after getting the value of the textarea.
Thanks for any help.
If regex is not mandatory then try
string = string.split(/\n|\r|↵/).join( "," );

Remove slashes from string using RegEx in JavaScript

I am trying to remove all special characters except punctuation from a customer complaint textarea using this code:
var tmp = complaint;
complaint = new RegExp(tmp.replace(/[^a-zA-Z,.!?\d\s:]/gi, ''));
but it keeps placing "/" in front, and in back of the string after sanitizing.
Example:
Hi, I h#ve a% probl&em wit#h (one) of your products.
Comes out like this
/Hi, I have a problem with one of your products./
I want
Hi, I have a problem with one of your products.
Thanks in advance for any help given.
The variable complaint is converted to a regular expression because you use the RegExp() constructor.
This probably isn't what you want. (I assume you want complaint to be a string).
Strings and regular expressions are two completely different data types.
Your output demonstrates how JavaScript displays regular expressions (surrounded by / characters).
If you want a string, don't create a regular expression (i.e. remove the RegExp constructor).
In other words:
complaint = complaint.replace(/[^a-zA-Z,.!?\d\s:]/gi, '');
You don't need the RegExp constructor:
complaint = tmp.replace(/[^a-zA-Z,.!?\d\s:]/gi, '');

How do I replace a double-quote with an escape-char double-quote in a string using JavaScript?

Say I have a string variable (var str) as follows-
Dude, he totally said that "You Rock!"
Now If I'm to make it look like as follows-
Dude, he totally said that "You Rock!"
How do I accomplish this using the JavaScript replace() function?
str.replace("\"","\\""); is not working so well. It gives unterminated string literal error.
Now, if the above sentence were to be stored in a SQL database, say in MySQL as a LONGTEXT (or any other VARCHAR-ish) datatype, what else string optimizations I need to perform?
Quotes and commas are not very friendly with query strings. I'd appreciate a few suggestions on that matter as well.
You need to use a global regular expression for this. Try it this way:
str.replace(/"/g, '\\"');
Check out regex syntax and options for the replace function in Using Regular Expressions with JavaScript.
Try this:
str.replace("\"", "\\\""); // (Escape backslashes and embedded double-quotes)
Or, use single-quotes to quote your search and replace strings:
str.replace('"', '\\"'); // (Still need to escape the backslash)
As pointed out by helmus, if the first parameter passed to .replace() is a string it will only replace the first occurrence. To replace globally, you have to pass a regex with the g (global) flag:
str.replace(/"/g, "\\\"");
// or
str.replace(/"/g, '\\"');
But why are you even doing this in JavaScript? It's OK to use these escape characters if you have a string literal like:
var str = "Dude, he totally said that \"You Rock!\"";
But this is necessary only in a string literal. That is, if your JavaScript variable is set to a value that a user typed in a form field you don't need to this escaping.
Regarding your question about storing such a string in an SQL database, again you only need to escape the characters if you're embedding a string literal in your SQL statement - and remember that the escape characters that apply in SQL aren't (usually) the same as for JavaScript. You'd do any SQL-related escaping server-side.
The other answers will work for most strings, but you can end up unescaping an already escaped double quote, which is probably not what you want.
To work correctly, you are going to need to escape all backslashes and then escape all double quotes, like this:
var test_str = '"first \\" middle \\" last "';
var result = test_str.replace(/\\/g, '\\\\').replace(/\"/g, '\\"');
depending on how you need to use the string, and the other escaped charaters involved, this may still have some issues, but I think it will probably work in most cases.
var str = 'Dude, he totally said that "You Rock!"';
var var1 = str.replace(/\"/g,"\\\"");
alert(var1);

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