I write a little function to inject pieces of templates to the HTML without the use of "big" templating engines like EJS, Mustache.js, Nunjucks, Pure.js, etc.
I pass this piece of code with a key-value object:
<div><b>#{firstname} #{lastname}</b>, #{gender}</div>
Now I need to remove the extra spaces if someone try to write "#{ firstname }" in place of "#{firstname}".
I'm sorry but I don't know how to write the right ReGex to remove the spaces. How can I remove the spaces?
Thanks for your support.
UPDATE:
Thanks to #Saleem for the answer. Here a demo with the fix:
https://jsfiddle.net/avq3sntq/
Try following regex:
result = subject.replace(/(#\{)\s*(\S+)\s*(?=})/img, "$1$2");
If you input string is:
<div><b>#{ firstname } #{lastname}</b>, #{gender}</div>
Output will be:
<div><b>#{firstname} #{lastname}</b>, #{gender}</div>
As you can see, removes all extra spaces round firstname inside curly braces. See Demo here
Related
I need to get an array of all strings that are contained in curly brackets using JavaScript.
{{text is here}}
note that the text could contain all special characters and could be multi line i have tried this so far
regex test
\{{(.*?)\}}
In your demo you enabled m flag which is a wrong flag here. You need s flag or even without flags:
{{([^]*?)}}
Note: You don't need to escape braces here.
Live demo
Try the following:
(?<=\{{)(.*?)(?=\}})
it works for
{{text is here}}
https://regex101.com/r/gYXSbO/7/
In React JSX how can I have the following text include a single quote? Or other punctuation that might need escaping?
return (<p>I've seen the movie.</p>)
Render as a JS string literal (with double-quotes):
return (<p>{"I've seen the movie."}</p>)
... or use the HTML entity for an apostrophe, ':
return (<p>I've seen the movie.</p>)
Nevermind, it works as is.
It was the IDE that was highlighting it as a mistake
You can use " html entity to have quote in your text.
<Text>I"ve seen the movie.</Text>
output: I"ve seen the movie.
or if want single quot use the below option:
<Text> I've seen the movie.</Text>
<Text>{'I\'ve seen the movie.'}</Text>
{/* you can use both ticks and single quotes depending on your use. */}
<Text>{`I've seen the movie.`}</Text>
output: I've seen the movie.
This is a great reason to use the backtick (`) for strings where it makes sense.
The text in the original question will work fine even though the syntax highlighting is off, but by also moving strings to a constant you can avoid worrying about escaping, highlighting and they're easier to find/update.
const TEXT_FOR_MOVIE = `Some text that's "quoted"`
const TEXT_FOR_MOVIE = Some text that's "quoted"
In case if you want to show a variable with quotes you can use this it will show quote and also display the value of the variable
{`"${variable}"`}
Put a "" in front of your apostrophe.
code:
"I can't reach it'
output:
I can't reach it
through queries to a Database I am retrieving such data that I previously inserted through HTML textarea or input. When I get the response from my DB , in a JSON object the text field looks like this :
obj : {
text : [some_text] ↵ [some_text]
}
I tried to replace with this function :
string_convert = function(string){
return string.replace("↵",'<br>')
.replace('&crarr','<br>')
.replace('/[\n\r]/g','<br>');
}
I have to show this string in HTML ,but it does not seems to work. I'm using UTF-8
Any advice?
The problem you have is that you have enclosed your regex in quotes. This is incorrect.
.replace('/[\n\r]/g','<br>');
^ ^
remove these two quotes
The quotes are unnecessary because the regex is already delimited by the slashes.
By putting quotes in there, you've actually told it that you want to replace a fixed string rather than a regular expression. The fixed string may look like an expression, but with the quotes, it will just be seen as a plain string.
Remove the quotes and it will be seen as an expression, and it will work just fine.
One other thing, though -- in order to make your regex work perfectly, I'd also suggest modifying it slightly. As it stands, it will just replace all the \n and \r characters with <br>. But in some cases, they may come together as a \r\n pair. This should be a single line break, but your expression will replace it with two <br>s.
You could use an expression like this instead:
/\r\n|\n|\r/g
Hope that helps.
you are missing the ending semicolons ; in your code:
string_convert = function(aString){
return aString.replace("↵",'<br>').replace('↵','<br>');
}
this does not necessary solve your problem, but it could likely.
From: Trying to translate a carriage return into a html tag in Javascript?
text = text.replace(/(\r\n|\n|\r)/g,"<br />");
I have a string of text I'm trying to parse with RegExp in JavaScript. Let's say it looks like this:
var myString = "This is a string of text (item item item) and more text here.";
I need to match the first occurrence of the word 'item' based on the fact that it is the first item inside the parentheses. I can't figure out how to write a pattern that will match ONLY the first item inside a set of parentheses.
Some of you may want to think of it like this: Pretend I'm parsing a string of Lisp and want to match all cars.
Thanks in advance.
If you can avoid checking for an opening parenthesis you could use a look ahead for the closing parenthesis.
item(?=[^\)]*\))
You can also use capturing groups with:
\(.*?(item).*?\)
EDIT
For a word that is repeated somewhere in the parentheses at least once:
\(.*?(\b\w+\b).*?\1.*?\)
EDIT 2
For just the first alphanumeric set of characters in a parentheses:
\([^\w]*(\b\w+\b).*?\)
Or a simpler alternative:
\(.*?\b(\w+)\b.*?\)
Use something like this:
/\((\w*?)(?:\s|\))/
Live example: http://tinkerbin.com/EJyjEX30 (click run to run the code))
You cannot make a pattern that doesn't match the opening parenthesis, since Javascript regexes don't support lookback.
You probably need to update your lexer code to understand capturing groups (or general functions or something convenient...) or you need to restructure you code so that it can deal with that extra paren you can't get rid of.
I'm looking for a javascript regex that will remove all content wrapped in quotes(and the qoutes too), in a string that is the outlook format for listing email addresses. Take a look at the sample below, I am a regex tard and really need some help with this one, any help/resources would be appreciated!
"Bill'sRestauraunt"BillsRestauraunt#comcast.net,"Rob&Julie"robjules#ntelos.net,"Foo&Bar"foobar#cstone.net
Assuming no nested quotes:
mystring.replace(/"[^"]*"/g, '')
Try this regular expression:
/(?:"(?:[^"\\]+|\\(?:\\\\)*.)*"|'(?:[^'\\]+|\\(?:\\\\)*.)*')/g
Here's a regex I use to find and decompose the quoted strings within a paragraph. It also isolates several attendant tokens, especially adjacent whitespace. You can string together whichever parts you want.
var re = new RegExp(/([^\s\(]?)"(\s*)([^\\]*?(\\.[^\\]*)*)(\s*)("|\n\n)([^\s\)\.\,;]?)/g);