I have the following code:
var string = "word1;word2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('\\w+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
// ends_with(string, ';') => ["word1", "word2"]
The function takes no regard to whitespace. For example if you input
ends_with('Jonas Sand,', ',')
the output will be Sand. Need help on making the function work with words that has whitespace.
You can use your separator within split and take all except the last part with slice:
function ends_with(string, character) {
return string.split(character).slice(0, -1);
}
\w matches word characters, use [^x] instead, where x is your character. This matches everything but your character.
So the first line in your function becomes
var regexp = new RegExp('[^' + character + "]+" + character, 'g');
on the other hand, if you want to match words separated by white space, use
var regexp = new RegExp('(\\w|\\s)+" + character, 'g');
PS: but isn't there a String#split function in javascript?
Try using '[\\w\\s]+' instead of '\\w+' to include whitespace.
Try the following:
var string = "word1;w ord2;word3,word4,word5,word6.word7";
function ends_with(string, character) {
var regexp = new RegExp('.+' + character, 'g');
var matches = string.match(regexp);
var replacer = new RegExp(character + '$');
return matches.map(function(ee) {
return ee.replace(replacer, '');
});
}
Related
I have the code:
var word = 'thisistheword';
var string = 'istheword';
console.log(word.replace(new RegExp(string, "gi"), "<b>"+string+"</b>"));
And now I get this<b>istheword</b>, but I want to receive inverted result, so: <b>this</b>istheword.
var word = 'thisistheword';
var string = 'istheword';
console.log(word.replace(new RegExp("(.*)" + string , "gi"), "<b>$1</b>" + string));
gives: <b>this</b>istheword
"(.*)" + string means match everything preceding string.
Wrapped in () so that you can refer to it later with the $1
I’d would to replace all “?” by “&” except the first one by javascript. I found some regular expressions but they didn’t work.
I have something like:
home/?a=1
home/?a=1?b=2
home/?a=1?b=2?c=3
And I would like:
home/?a=1
home/?a=1&b=2
home/?a=1&b=2&c=3
Someone know how to I can do it?
Thanks!
I don't think it's possible with regex but you can split the string and then join it back together, manually replacing the first occurance:
var split = 'home/?a=1?b=2'.split('?'); // [ 'home/', 'a=1', 'b=2' ]
var replaced = split[0] + '?' + split.slice(1).join('&') // 'home/?a=1&b=2'
console.log(replaced);
You could match from the start of the string not a question mark using a negated character class [^?]+ followed by matching a question mark and capture that in the first capturing group. In the second capturing group capture the rest of the string.
Use replace and pass a function as the second parameter where you return the first capturing group followed by the second capturing group where all the question marks are replaced by &
let strings = [
"home/?a=1",
"home/?a=1?b=2",
"home/?a=1?b=2?c=3"
];
strings.forEach((str) => {
let result = str.replace(/(^[^?]+\?)(.*)/, function(match, group1, group2) {
return group1 + group2.replace(/\?/g, '&')
});
console.log(result);
});
You can split it by "?" and then rewrap the array:
var string = "home/?a=1?b=2";
var str = string.split('?');
var new = str[0] + '?'; // text before first '?' and first '?'
for( var x = 1; x < str.length; x++ ) {
new = new + str[x];
if( x != ( str.length - 1 ) ) new = new + '&'; //to avoid place a '&' after the string
}
You can use /([^\/])\?/ as pattern in regex that match any ? character that isn't after / character.
var str = str.replace(/([^\/])\?/g, "$1&");
var str = "home/?a=1\nhome/?a=1?b=2\nhome/?a=1?b=2?c=3\n".replace(/([^\/])\?/g, "$1&");
console.log(str);
I am trying to replace a particular string using regex.
var replace = {'<RAndom>': "random object"};
I am replacing it using the dynamic regex because i have a lot of objects.
var tagsText = "<RAndom> hellow world";
var regex = new RegExp('\\b(' + Object.keys(replace).join('|') + ')\\b', 'g');
tagsText = tagsText.replace(regex, function(match) {
return replace[match] + match;
});
But it is not working.I think the problem is with the semicolon but i am not sure.The output is again the same.
"<RAndom> hellow world"
Any ideas?
Problem is presence of \b (word boundary) on each side that is placed before & and ;. Both & and ; are not non-word characters and \b cannot be asserted before and after non-word chars.
You can use \B instead:
var regex = new RegExp('\\B(' + Object.keys(replace).join('|') + ')\\B', 'g');
and then
tagsText = tagsText.replace(regex, function(match) {
return replace[match] + match;
});
//=> "random object<RAndom> hellow world"
The word boundary \b and non-word boundary assertion behavior depends on the context. Make it context-independent with unambiguous (^|\W) and ($|\W):
var replace = {'<RAndom>': "random object"};
var tagsText = "<RAndom> hellow world";
var regex = new RegExp('(^|\\W)(' + Object.keys(replace).join('|') + ')(\\W|$)', 'g');
tagsText = tagsText.replace(regex, function(match, g1, g2, g3) {
return replace[g2] ? replace[g2] + match : match;
});
// And just a demo below
document.body.innerHTML = "<pre>" + tagsText.replace(/&/g, '&') + "</pre>";
The (^|\W) will match the start of string or a non-word character. The ($|\W) will match the end of the string or a non-word character.
Since we have 3 groups now, we can pass them as arguments to the replace callback. With replace[g2] ? replace[g2] + match : match;, we first check if there is a value for g2 key, and if yes, perform the replacement. Else, just return the match.
I want to search this word: '#foo'
in this string:
first foo second #foo
so that only the last '#foo' (with '#') will match
I use this code and it causes two mistakes:
var mySearch ='#foo'
var regexp = new RegExp('\\b' + mySearch + '\\b', 'g');
searching mySearch = 'foo' will result a match to both "foo" (wrong! - should match only the first)
searching mySearch = '#foo' will result no hit at all (wrong - should match the second)
how to write it right?
Try:
var regexp = new RegExp('\b' + mySearch + '\b', 'g');
\b in regex is boundary of word. It will fail to match the boundary between a space and a pound #.
So your regexp should just be
var regexp = new RegExp(mySearch + '\\b', 'g');
How can I apply multiple regexs to a single string?
For instance, a user inputs the following into a text area:
red bird
blue cat
black dog
and I want to replace each carriage return with a comma and each space with an underscore so the final string reads as red_bird,blue_cat,black_dog.
I've tried variations in syntax along the lines of the following so far:
function formatTextArea() {
var textString = document.getElementById('userinput').value;
var formatText = textString.replace(
new RegExp( "\\n", "g" ),",",
new RegExp( "\\s", "g"),"_");
alert(formatText);
}
You can chain the replacements. Every application of the replace method retuns a string, so on that string you can apply replace again. Like:
function formatTextArea() {
var textString = document.getElementById('userinput').value;
var formatText =
textString.replace(/\n/g,",")
.replace(/\s/g,"_");
alert(formatText);
}
There's no need for all these new Regular Expression Objects by the way. Use Regular Expression literals (like /\n/g in the above).
Alternatively you can use a lambda for the replacement
const str = `red bird
blue cat
black dog`;
console.log(str.replace(/[\n\s]/g, a => /\n/.test(a) ? "," : "_"));
formatText = textString.replace(/\n/g,',').replace(/\s/g,'_');
As others have mentioned, chaining is good enough for something as simple as what you're asking. However, if you want this to be more dynamic, you can use a replacer function as the second argument:
function formatTextArea() {
var textString = document.getElementById('userinput').value;
var formatText = textString.replace(/\n|\s/g, function ($0) {
if ($0 === "\n")
return ",";
else if ($0 === " ")
return "_";
}
alert(formatText);
}
Using a replacer function will allow you to be dynamic without having to chain together calls to replace(). It may also be marginally faster (regex parser is invoked only once). Be aware that \s will match more than just the space character, though :-) For the purposes of your question, this would be good enough:
var formatText = textString.replace(/\n|\s/g, function ($0) {
return $0 == "\n" ? "," : "_";
}
Regexp object have their own literal notation, using forward slashes, meaning that backslashes don't have to be escaped. For example, /\n/g is equivalent to new RegExp('\\n','g').
function formatTextArea()
{
var textString = document.getElementById('userinput').value;
textString = textString.replace(/\n/g,",").replace(/\s/g,"_");
alert(textString);
}
var textString = "red blue\nhack bye\ntest rt\n";
var formatText = textString.replace(new RegExp( "\\n", "g" ),",").replace(new RegExp( "\\s", "g"),"_");
alert(formatText);
Include http://phpjs.org/functions/str_replace:527 and then
input = str_replace("\\n", ',', input);
input = str_replace(' ', '_', input);