Find String that starts and ends with a specific char [duplicate] - javascript

This question already has answers here:
Regex to match a string with specific start/end
(3 answers)
Closed 4 years ago.
I would like to find a string that starts and ends with a specific special character.
I tried the following regex but its not working:
(\#*\.|\&)[A-Za-z]+\.*#
I want to find any string that starts with #* and ends with *# but can't find the right regex for it.
Sample :
Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#
I am trying to find the string #*DCSN_RSN*# in the above string and replace it with <p>#*DCSN_RSN*#</p>

I think this is what you are looking for:
var sample = 'Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#';
var replaced = sample.replace(/(#\*[\s\S]*?\*#)/g, '<p>$1</p>');
console.log(replaced);
To match any characters you can use [\s\S]*, then add ? for [\s\S]*? to make it less-greedy. Your * characters also need to be escaped.

Related

Remove single quotes and last comma in jquery string using Regex [duplicate]

This question already has answers here:
JavaScript REGEX Match all and replace
(2 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a data
'abc','def','ghi',
I want to remove the single quote on all character and want to remove only the last comma, example like below
abc,def,ghi
How would i achieve that using regex for javascript?
I tried using this regex
.replace(^\'|,\s*$,"");
But seems like it is only removing the first quote as shown below
abc','def','ghi',
I am not very good in regex, i appreciate any help that i can get. Thanks
try this:
.replace(/\'|,$/g, "");
the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match
There is an easy way, use the $ operator
.replace(/'|,$/g, '')
Can you please check replace(/'|(,)$/g," ") and it should work.

Javascript: Regex how to match URL anywhere in a string [duplicate]

This question already has answers here:
Regular Expression to find URLs in block of Text (Javascript)
(4 answers)
Closed last month.
Some possible sting like:
https://www.google.com
End: https://g.co
Inside https://b.co and https://c.co
How to match the URL anywhere in a string?
The URL may at the end of a string or end of space inside a string.
Currently I'm using the Regex like:
/(https:\/\/.*?)[$|\s]/gi
But it seems could not match the URL at the end of a string that the '$' does not work here.
Try this
/(https?:\/\/\S+)/gi
Or in your way
/(https:\/\/.*?)($|\s)/ig
To make things clear, anything inside "[]" already implies a "|". Adding it in "[]" will result in matching the character "|". Also $ inside the "[]" is considered as the char "$" and not as the end of string because of which your regex doesn't match the URL at the end of text.

How to fix my regex? [duplicate]

This question already has answers here:
Matching a Forward Slash with a regex
(9 answers)
Closed 8 years ago.
I tried making a regex expression to search for in the string named 'patt'. Unfortunately the following gives an error in dreamweaver:
patt.search(/.*://.*waw\d.omegle.com/);
I need to get this pattern working. What am i doing wrong here?
You need to escape the / in the pattern as
patt.search(/.*:\/\/.*waw\d\.omegle\.com/);
Also escape . for more saftey as . alone could match anything in regex
Example
var patt = "http://asdfwaw1.omegle.com";
patt.search(/.*\/\/.*waw\d\.omegle\.com/);
=> True
because it thinks the / in // is the end of the reg exp. If you look at the coloring in your code above you can see the brown color ends at the first slash. You need to escape it.
/.*:\/\/.*waw\d.omegle.com/;

Wanted to check whether string ends with special character or not? [duplicate]

This question already has an answer here:
Need to do a right trim on ajax query in javascript?
(1 answer)
Closed 8 years ago.
I wanted to check whether my string which ends with special character or not. If my string contains special character at the end, then need to trim at the right. if not, do nothing.
My piece of code:
var s = 'acbd#';
var x = 'abcd#e'
Expected Result:
acbd
abcd#e
any help on this?
You can use regular expression to replace the special characters with empty string, like this:
s.replace(/[#]+$/, "");
x.replace(/[#]+$/, "");
You can specify more special characters inside of square brackets.

Matching content within a string with one regex [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Simple way to use variables in regex
(3 answers)
Closed 10 years ago.
I am looking for a way to RegEx match for a string (double quote followed by one or more letter, digit, or space followed by another double quote). For example, if the input was var s = "\"this is a string\"", I would like to create a RegEx to match this string and produce a result of [""this is a string""].
Thank you!
Use the RegExp constructor function.
var s = "this is a string";
var re = new RegExp(s);
Note that you may need to quote the input string.
This should do what you need.
s =~ /"[^"]*"/
The regex matches a double quote, followed by some number of non-quotes, followed by a quote. You'll run into problems if your string has a quote in it, like this:
var s = "\"I love you,\" she said"
Then you'll need something a bit more complicated like this:
s =~ /"([^"]|\\")*"/
I just needed a pattern to match a double quote followed by one or more characters(letters, digits, spaces) followed by another double quote so this did it for me:
/"[^"]*"/

Categories

Resources