How to escape the double forwardslash in regex (javascript) [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
How to replace the specific string only using one replace instead of two?
const formattedUrl = url.replace('flashget://', '').replace('&abc','')
What I have tried: (Not Working)
const formattedUrl = url.replace(/flashget:\/\/ | &abc/g, '').replace('&abc','')
Example
Input Url: flashget://W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=&abc
Formatted Url: W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=

Take out the spaces around the |

This is my attempt:
https://regex101.com/r/eFO7Eh/2
Search Regex:
flashget:\/\/(.*)\&.*$
Replace term:
$1
Just pay attention to the fact that this is a different logic and requires handling capture groups.

remove the space before and after the or |. it will work.
let url = "flashget://W0ZMQVNIR0VUXWh0dHA6Ly93d3cuZm9yZWNlLm5ldC93aW43LnJhcltGTEFTSEdFVF0=&abc"
const formattedUrl = url.replace(/flashget:\/\/|&abc/g, '');
console.log(formattedUrl);

Related

Remove space between words in string javascript js React [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm using React (hooks) and in a component I have postcodes with a space in them in strings:
eg "B72 1JL".
I need to remove the space in the middle of the postcodes so it is "B721JL" and also possibly any spaces before or after the postcode.
I've googled for ages and cannot find anything that will work. I know i probably need regex...but pretty confused!
Help appreciated.
Use String.prototype.replace() to change the space for an empty space.
const str = "B72 1JL";
// Replacing " " (space) to "" empty space
const res = str.replace(/ /g, '')
console.log(res); // BJ721JL

Javascript to extract certain string from data [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I need to find a regex to extract first occurrence of a string from a data.
For example my data is like :
SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD
I need to extract ABCD123456789 from this data.
I need to find first occurrence of string always starts with ABCD and has total length of 13.
How can I achieve this with using regex?
I tried regex /^ABCD(\w{9})$/gm which didn't work for me.
You can use /ABCD\w{9}/g with match() to get the result from first index:
var str = "SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD"
console.log(str.match(/ABCD\w{9}/g)[0])
The pattern that you tried ^ABCD(\w{9})$ does not match because you use anchors ^ and $ to assert the start and the end of the string.
Note that if you want a full match only, you don't need a capturing group (\w{9})
You can omit those anchors, and if you want a single match from the string you can also omit the /g global flag and the /m multiline flag.
ABCD\w{9}
Regex demo
const regex = /ABCD\w{9}/;
const str = `SFASDŞŞVMA SAD SADAS MYABCD12345678911TY ISIADABCD12345678911SAD`;
console.log(str.match(regex)[0])

How do I remove brackets and bracketed contents? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I tried .replace(/ *\[[^)]*\] */g, ""); and it works for instances where there's only a pair of brackets
"[Dialog4]Hello, this is Mike"
but doesn't work for
"[Dialog4]Hello, this is Mike[Dialog5]"
because it just removes entire thing
The result should be
"Hello, this is Mike"
use not greedy mode in regex:
\[.*?\]
here is a tester: https://regex101.com/r/NyireC/1
You can use
\[[^\]]*\]
let str = "[Dialog4]Hello, this is Mike[Dialog5]"
let replaced = str.replace(/\[[^\]]*\]/g,"")
console.log(replaced)
Your regex is almost there.
You don't need the space+* at the start and end, because you only want to replace the square brackets and their contents, not anything before/after it.
In the negated character class, you are negating ), where you should be negating ] instead. This is possibly a typo.
With these modifications, the regex becomes:
\[[^\]]*\]
Demo
Perhaps a bit sloppy, but you could use the regex /\[(?<=\[)[^\]]*(?=\])]/g.
This makes use of both a positive lookbehind and positive lookahead, on the [ and ] characters respectively.
const string = "[Dialog4]Hello, this is Mike[Dialog5]";
const regex = /\[(?<=\[)[^\]]*(?=\])]/g;
const output = string.replace(regex, "");
console.log(output);

Concatenate HTML [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I'm building my output text looping a JSON object using JavaScript. Everything is working fine until I come to the part where I need to add an ID number to an image URL. My images are stored in a database and I am using an ASHX handler to load the image however what I end up with is not exactly what I need.
My code that I need to end up with is ~/ImageHandler.ashx?id=35
but what I get is ~/ImageHandler.ashx?id='35'. Single quotes around the ID.
I know it is the syntax of " and "".
What I have tried is
myOutput += "<img src ='~/ImageHandler.ashx?id='" + ID + class='person-image'></img>"
and every combination that does not work.
If I remember correctly I need some combination of triple single quote or double quotes something or some combination of the both. ID is an integer that is being read from a JSON object.
Thanks in advance
You can add double quotes using the escape character \
myOutput += "<img src=\"~/ImageHandler.ashx?id=" + ID + "\" class=\"person-image\"></img>"
This will append
<img src="~/ImageHandler.ashx?id=35" class="person-image"></img>

Issue With Replacing RegExp Terms In String [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
var testString = "This string has a bad word in it to test";
function findBadWords(string) {
var badWord = /\bbad\b | \bword\b | \btest\b/gi
var isBadWord = string.match(badWord);
if (isBadWord) {
newString = string.replace(badWord," *** ");
}
document.write(newString);
}
findBadWords(testString);
So I'm practicing with RegExp's currently and I have run into a problem I don't understand. In the code above, I have set a RegExp to find "bad words" in a string. From what I can tell, I have set it to find the word "bad", "word", and "test" as long as there is a word boundary before and after the word. The issue I'm having is that "word" isn't being replaced. If I put a non-badWord before "word" it gets replaced, but not otherwise. I have tried taking off some of the word boundaries or adding some non-word boundaries with no luck. Would anyone mind explaining why this code is working the way that it is and how I could fix it?
Thanks!
Also, I know using document.write is a poor choice but it's only for testing I swear!
The issue here is the \b alongside the " " empty space character. If you remove the spaces from your regex it works well.
var testString = "This string has a bad word in it to test";
function findBadWords(string) {
var badWord = /\bbad\b|\bword\b|\btest\b/gi
var isBadWord = string.match(badWord);
if (isBadWord) {
newString = string.replace(badWord," *** ");
}
document.write(newString);
}
findBadWords(testString);

Categories