This question already has an answer here:
JS regex replace not working
(1 answer)
Closed 5 years ago.
I'm trying to get only AN from this string, but replace function seems not working, I end up getting exactly the same string every time... any hits?
building = "\n AN ";
var temp = building.replace("\\W", "");
You need the meta sequence any non-word character \W and the global flag g for a continuing replacement.
var building = "\n AN ",
temp = building.replace(/\W/g, "");
console.log(temp);
Related
This question already has answers here:
JavaScript split String with white space
(8 answers)
Closed last year.
In my input field I have sometimes words that contain apostrophe or need to be searched using quotes.
Example of searched word: "de l'article".
I need to separate them by space, so I will have "de" and "l'article".
I tried:
let word = document.getElementById("searchedWord").value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let x = new RegExp("(" + word.replace(/\s+[']+/g, "|") + ")", "ig");
It doesn't seem to work properly. Can anyone help me write this regular expression?
You should check out the split function :)
for example in order to split on every space
let x = "This is an example";
const myArray = x.split(' ');
let word = myArray[1]
word = 'This'
myArray = [This,is,an,example]
This question already has answers here:
Remove all special characters except space from a string using JavaScript
(13 answers)
Closed 4 years ago.
I need to remove all special characters from string using Javascript but its unable to remove. Please find my code below.
function checkString(){
var sourceString='a|"bc!#£de^&$f g';
var outString = sourceString.replace(/[`~!##$%^&*()|+\-=?;:'",<>\{\}\[\]\\\/]/gi, '');
console.log('sourcestring',outString);
}
Here I could not get the expected output. I am getting this abc£def g in console. Here I need to remove all special characters. Please help me to resolve this issue.
Use regex:
var sourceString='a|"bc!#£de^&$f g';
console.log("Before: " + sourceString);
sourceString = sourceString.replace(/[^a-zA-Z0-9 ]/g, "");
console.log("After: " + sourceString);
It essentially removes everything but alphabet and numbers (and spaces).
Remove every thing except numbers and letters.
var sourceString='a|"bc!#£de^&$f g';
// var outString = sourceString.replace(/[`~!##$%^&*()|+\-=?;:'",<>\{\}\[\]\\\/]/gi, '');
var outString = sourceString.replace(/[^a-zA-Z0-9]/g, '');
console.log('sourcestring',outString);
This question already has an answer here:
Extract until end of line after a special character: Python
(1 answer)
Closed 5 years ago.
I have a string want to get string after special character which is extension.
i tried and it is works fine in javascript but wasnt in python.. how to do that?
here is my JS snippet,
my_str_0 = ".exr[6,7]";
my_str_1 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%04d.exr[6] 1-7";
my_str_2 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%05d.png[1] 1-10";
my_str_3 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%05d.jpg";
for (var i in [my_str_0, my_str_1, my_str_2, my_str_3]) {
var reg = /([^.]*)$/.exec(eval("my_str_"+i));
console.log(reg[0]);
}
here is my python regex link
Use MultiLine Flag.
See here in this link
This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'
This question already has answers here:
How to grab substring before a specified character in JavaScript?
(12 answers)
Closed 6 years ago.
I am attempting to take the city out of a city, state string. For instance, "Portland, OR" would need to equal "Portland". How can I use Javascript or a regular expression to do this?
var myString = "Portland, OR";
I want to extract everything up to the comma but not including the comma.
var city = "Portland, OR".split(",")[0];
With regex:
var regex = /^[^,]+/;
var city = regex.exec("Portland, OR");
This is the regex version
var result = myString.match(/^[^,]+/)
UPDATE
This one always returns a value without error
var result = String(myString || "").match(/^[^,]*/)[0]