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

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, "{");

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 extract numbers from string in Javascript using regex [duplicate]

This question already has answers here:
Find and get only number in string
(4 answers)
Closed 8 years ago.
I have the following string
/Date(1317772800000)/
I want to use a Javascript regular expression to extract the numerical portion of it
1317772800000
How is this possible?
That should be it
var numPart = "/Date(1317772800000)/".match(/(\d+)/)[1];
No need for regex. Use .substring() function. In this case try:
var whatever = "/Date(1317772800000)/";
whatever = whatever.substring(6,whatever.length-2);
This'll do it for you: http://regex101.com/r/zR0wH4
var re = /\/Date\((\d{13})\)\//;
re.exec('/Date(1317772800000)/');
=> ["/Date(1317772800000)/", "1317772800000"]
If you don't care about matching the date portion of the string and just want extract the digits from any string, you can use this instead:
var re = /(\d+)/;
re.exec('/Date(1317772800000)/')
["1317772800000", "1317772800000"]

regex search using string [duplicate]

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");');

Categories

Resources