Bad Character in Regex - javascript

i ran into a simple problem, which I think you guys can solve. I am programming javascript, where I use the following code to replace all strings in a string with another string:
str = str.replace(/find/g,”replace”)
Yes, the code works, but what I want to do is:
str = str.replace(/</p>/g,”replace”)
It won't work because of:
</p>.
It dosen't like the /.
Anyone who can help me?

Use an escape:
str.replace(/<\/p>/g, "replace");
^--- escape char.

Escape / with another \. Like <\/p>
Should work.

Related

Split the string using comma but ignore the comma within double quotes - javascript

I am aware of the fact that, this has been asked before in this forum - Split a string by commas but ignore commas within double-quotes using Javascript. But my requirement is slight different which has been asked here. Sorry for confusing you with my question.
I have a string like below -
myString = " "123","ABC", "ABC,DEF", "GHI" "
Here I want to split this string by comma and store it to an array but ignoring the comma within the double quote. Here is what I have tried so far.
myArray.push(myString.replace(/"/g,"").split(","));
But I'm not sure how to ignore the ',' inside the double quote. Could anyone please help ?
This is how my output should look like -
myArray = ["123","ABC", "ABC,DEF", "GHI"]
Seems like you already have an array, so there's no comma to split. Converting it .toString() looks like an unnecessary step.
I think you're confusing language syntax with actual data. The double quotes you see aren't part of the content of the strings. It looks like you already have the data you need.
One way to do it with the help of split(), map() and replace()
myString = ' "123","ABC", "ABC,DEF", "GHI" ';
console.log(myString.split('",').map(n=>n.replace(/\"| /g,'')));
myString = ' "123","ABC", "ABC, DEF", "GHI" ';
console.log(JSON.parse(`[${myString}]`));
This should do the trick... Basically what epascarello wrote in the comments to the questions.
Cheers.
You can try with the regex ([^"])*
I did test it on https://regex101.com
This is the result you can see in the image below => https://regex101.com/r/a5dwNY/1

How to make replace() global in JavaScript

I know this question had been asked lot of time but i could not find solution. I have some smilies which each of them has code to be rendered as smiley using replace() , but I get syntax error, I don't know why and how to render my code :/ to smiley
txt = " Hi :/ ";
txt.replace("/\:/\/g","<img src='img/smiley.gif'>");
Your regular expression doesn't need to be in quotes. You should escape the correct / forward slash (you were escaping the wrong slash) and assign the replacement, since .replace doesn't modify the original string.
txt = " Hi :/ ";
txt = txt.replace(/:\//g,"<img src='img/smiley.gif'>");
Based on jonatjano's brilliant deduction, I think you should add a little more to the regular expression to avoid such calamities as interfering with URLs.
txt = txt.replace(/:\/(?!/)/g,"<img src='img/smiley.gif'>");
The above ensures that :// is not matched by doing a negative-lookahead.
There are two problems in the first argument of replace() it escapes the wrong characters and it uses a string that seems to contain a regex instead of a real RegExp.
The second line should read:
txt.replace(/:\//g,"<img src='img/smiley.gif'>");
/:\//g is the regex. The first and the last / are the RegExp delimiters, g is the "global" RegExp option (String.replace() needs a RegExp instead of a string to do a global replace).
The content of the regex is :/ (the string you want to find) but because / has a special meaning in a RegExp (see above), it needs to be escaped and it becomes :\/.

Javascript replace expression explained

I have the following function:
function replace(path) {
return path.replace(/\//g, '.').replace(/^\./, '');
};
Can you please explain what exactly is doing? I have some hard time understanding it mostly because of the slashes and escapes.
I know it replaces something with something. :)
return path.replace(/\//g, '.').replace(/^\./, '');
The / at the start and end are delimiters of regex.
The \ inside it will escape the following character(\ and .).
/\//g: Will find all(g: global flag) the / in the string and will replace it by .
/^\./: Will find the . at the start(^) of the string and will remove it
Replace / with ., and then replace . at the beginning of string with an empty string.
Let's assume that path is a string. strings in javascript, like everything else, are objects. Among other things, they have a replace function. You can read about it here.
Those things with the slashes are called regular expressions, or RegExs for short. You can read about them here and here. They are very useful and often used for manipulating strings with operations like replace.

replacing special characters with a specific special character

I have a string which is exactly like this...
R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%2fSka,
I have tried enough to remove the special characters before they reach the browser...but not going anywhere..so planing to rely on my old and trusted friend "javascript" I want it to read
R&B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae&Ska,
I know this can be done through regular expression which I am just not able to figure it out. How would I write the expression?
Any help would be highly appreciated
You may try using:
decodeURIComponent("R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%26Ska")
//^prints^ "R&B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae&Ska"
Look at these answers:
Regex to remove all special characters from string?
They layout a regex that will remove everything EXCEPT those characters you want to allow, this is safer then removing a list of %26,%2f, etc.
For example...
[^0-9a-zA-Z, ]+ would allow all letters, numbers, commas and whitespace.
[^0-9a-zA-Z]+ would be only letters and numbers
The other answers are probably pointing you in a better direction... if it means fixing the string before it gets to the client.
Probably you need decodeURIComponent() function.
<script>
var decodedString = decodeURIComponent('R%26B,Alternative,Rock,Classic Rock,Heavy Metal,Classical,Reggae%2fSka');
</script>
http://www.w3schools.com/jsref/jsref_decodeuricomponent.asp

RegEx not working in Javascript: SCRIPT5018: Unexpected quantifier

I'm trying to make a RegEx that can single out words from a string but ignore them if they're inside a tag. For example: Even though the searchword is SPAN, do not replace a span tag.
What I have so far is:
(?<![<\/])\bspan\b(?!>)
http://regex101.com/r/vS6yG6
Span obviously is a placeholder. In the script it is generated from a dictionary dynamically.
This is what I'm trying to run:
var reg = new RegExp(the expression, 'gi');
I've escaped the /, so I'm not sure where the problem is.
And this is what I get back: SCRIPT5018: Unexpected quantifier
Any help would be appreciated. I made the Regular Expression with the help of regex101.com.
Try this ...
/>[^<>]*\b(span)\b[^<>]*<?/ig
Like David replied, there is no Negative LookBehind in Javascript, so that's where the problem was.
http://blog.stevenlevithan.com/archives/mimic-lookbehind-javascript

Categories