regex search using string [duplicate] - javascript

This question already has answers here:
how to pass a variable into an regular expression in javascript [duplicate]
(3 answers)
Closed 8 years ago.
I have a string of text that has the value of the selected text. Id like to do a replace() of this text in a given div of text.
so I know I can do that with the following:
myelement.replace(/foo/g, 'bar');
However I need to do it with my string ie:
myelement.replace(/*mystring*/g, 'bar');
So I tried:
mystring = '/'+mystring+'/g';
myelement.replace(mystring, 'bar');
Which didnt work, so i tried (which i knew wouldn't work):
myelement.replace(/+mystring+/g, 'bar');
So how can i do this?
I've coded something up for you guys in jsfiddle --> HELP ME PLEASE!

http://jsfiddle.net/2rG2V/
The change being to use new RegExp(st, "g") instead of creating the string as you did before. /test/g is just a shortcut way of creating a RegExp object.

simply replace your
regSt = '/'+st+'/g';
with
regSt = new RegExp(st,'g');
more info on regExp http://www.w3schools.com/jsref/jsref_obj_regexp.asp

eval('myelement.replace(/' + mystring + '/g, "bar");');

Related

find and replace '%20' with a space in a string javascript [duplicate]

This question already has answers here:
Javascript replace all "%20" with a space
(7 answers)
Closed 4 years ago.
I'm having some trouble trying to figure this out,
basically I have a url string like so this%20is%20a%20string now what I want to do is find and replace all instances of %20 and replace with a space so the string then becomes this is a string.
Now I've tried to do something like this..
if(string.includes('%20')) {
const arr = str.split('%20');
}
which splits the string into an array, but I'm not sure how I can then turn the array of seperate strings into a full string with spaces between each word.
Any help would be appreciated.
Using regex,
str.replace(/%20/g, ' ');
Just use join:
str.split('%20').join(" ")
let val = "this%20is%20a%20string".replace(/%20/g, ' ');
alert(val);
replace

How do I insert something at a specific character with Regex in Javascript [duplicate]

This question already has answers here:
Simple javascript find and replace
(6 answers)
Closed 5 years ago.
I have string "foo?bar" and I want to insert "baz" at the ?. This ? may not always be at the 3 index, so I always want to insert something string at this ? char to get "foo?bazbar"
The String.protype.replace method is perfect for this.
Example
let result = "foo?bar".replace(/\?/, '?baz');
alert(result);
I have used a RegEx in this example as requested, although you could do it without RegEx too.
Additional notes.
If you expect the string "foo?bar?boo" to result in "foo?bazbar?boo" the above code works as-is
If you expect the string "foo?bar?boo" to result in "foo?bazbar?bazboo" you can change the call to .replace(/\?/g, '?baz')
You don't need a regular expression, since you're not matching a pattern, just ordinary string replacement.
string = 'foo?bar';
newString = string.replace('?', '?baz');
console.log(newString);

Javascript regex in attribute [duplicate]

This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)

How to replace "{ with { in string using Javascript? [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
Can anyone tell me how to replace "{ with { using JavaScript?
Here is what I am trying to do:
string.replace(/\"\{/g, "{");
Your regex is fine. Don't forget that strings are immutable in javascript. The replace function doesn't change the receiver string but builds a new one.
So you must do
string = string.replace(/\"\{/g, "{");
In case you were using this directly on string you should use it on an instance of string. Not on string type.
(I know it sounds too trivial, but otherwise this code should have worked. :) )
var stringTypeVariable = 'some string "{ with target pattern';
var replacedVariable = stringTypeVariable.replace(/\"\{/g, "{");

Regular Expression only returning first result found [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g

Categories

Resources