JavaScript Strip Vowels - javascript

I am trying to strip vowels in a string. I know I should be using str.replace but I am baffled on how to get it into a span.
This is more of less what i am looking to do:
Write a JavaScript function that takes in a string s and returns the string which is equivalent to s but with all ASCII vowels removed. EX: (“Hello World”) returns: "Hll wrld"
Please help!

.replace(/[aeiou]/ig,'') is all you need.

To replace vowels you can use a simple regular expression:
function removeVowels(str) {
return str.replace(/[aeiou]/gi, '');
}
As for the part about getting it into a span, not 100% sure what you mean, but maybe something like this:
<span id="mySpan">Hello World!</span>
<script>
var span = document.getElementById('mySpan');
span.innerHTML = removeVowels(span.innerHTML);
</script>

string.replaceAll("[aeiou]\\B", "")

Related

Javascript replace text within <title> with <span>title</span>

Hi i want to replace my javascript string solution like this.
input:- this is <title> and <heading>
output:- this is <span>title</span> and <span>heading</span>
can anyone help me on this.
Thanks in advance.
use regex to replace
input.replace(/</g, '<span_').replace(/>/g,'</span>').replace(/_/g,'>');
Try something like this:
var input = "this is <title> and <heading>";
var output = input.replace(/<([^>]+)>/g, "<span>$1</span>");
console.log(output);
That is, match an opening <, followed by one or more non-> characters captured as a submatch so that you can reference it as $1 in the replacement string, followed by a closing >.
Another alternative
var output = input.split("<").join("#").split(">").join("</span>").split("#").join("<span>");
console.log(output);
Use
input.replace(/<[a-zA-Z]*>/g, '<span>$&</span>');
Explanation:
.replace encloses all tags in ''. '$& is replaced with the tag. See this for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
Example:
var ta = document.getElementById('ta'), output = document.getElementById('output');
ta.value = 'I have <title> and <head> (this is just example text, you can replace).';
function replaceIt() {
output.innerText = ta.value.replace(/<[a-zA-Z]*>/g, '<span>$&</span>');
// Okay, technically Node#innerText is non-standard, so use an HTML escape function on production sites.
// For demo purposes, I just chose innerText for ease of use
}
<textarea id="ta"></textarea>
<button onclick="replaceIt()">Enclose tags in &ltspan&gt s.</button>
<div>Output:</div>
<div id="output"></div>

How can I get words from a string, textarea javascript

I've been working on JavaScript and HTML, I have a text area where the user sets a CSV like this:
17845 hello bye 789
Now I have 17845,hello,bye,789 and I need to extract the values between the commas. I've tried with index Of, but what if the user sets 2 lines instead of 1, how can I get these words? I have thought of separate them getting the "\n".
Javascript function split() will do the trick
var str = '17845,hello,bye,789';
var words = str.split(',');
console.log(words);
Use javascript split() Function
Split function gives u the array.
var sentence ="hello, 123, tedsfd, demo";
var strArr = sentence.split(',');
$.each(strArr,function(key,value){
console.log(value);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
If I understand it correctly, you are facing issues if the user sets comma separated value in multiple lines instead of one... For that use trim() function to remove all tabs and newline and then use split() function.
Use String.split.It converts strings into arrays, given you provide the delimiter, to separate the string.
var userInput = '17845, hello, bye, 789';
data = userInput.split(',');
console.log(data);
//data[0] = '17845'
//data[1] = 'hello'
//data[2] = 'bye'
//...

replace two same character with whitespace

I am trying to replace the string below with whitespaces using javascript
function replaceString()
{
var str = "ABC**EFG";
return str.replace(/\*/g, " ");
}
I received the result as ABC EFG but I expect the result to come with two whitespace.
I also tried the same thing using php str.replace but still get the same result.
Is there any other methods i can used to replace the individual asterisk with whitespace??
P/S: The return string will be used as part of the sql query
[UPDATE]
I ended up return the string without any replacement to sql, then I use sql replace function to perform replacement in the query.
If you're displaying the resulting string in an HTML element then two or more whitespaces will be displayed as only one whitespace. To workaround this fact, use instead:
return str.replace(/\*/g, ' ');
try this
return str.replace(/\*/g, ' ');
Buddy the problem is with the HTML compiler which has its own special rules of parsing
So it parses multiple spaces into one.This can work for HTML only.
Thats why use the GIFT tag .
<pre>
<p id="para"></p>
</pre>
<script> function replaceString()
{
var str = "ABC**EFG";
return str.replace(/\*/g," ");
}
document.getElementById("para").innerHTML=replaceString();
</script>
<script>
function replaceString()
{ var str1=String.fromCharCode(32,32);
var str = "ABC**EFG";
return str.replace(/\*/g,str1);
}
alert(replaceString());
</script>
return of function from above code can be used directly in mysql...
Finally found the best solution, I replace those special characters using percent-encoding (URL-encoding)
for my case: str.replace(/\*/g, "%20");

Simple Javascript string manipulation

I have a string that will look something like this:
I'm sorry the code "codehere" is not valid
I need to get the value inside the quotes inside the string. So essentially I need to get the codehere and store it in a variable.
After some researching it looks like I could loop through the string and use .charAt(i) to find the quotes and then pull the string out one character at a time in between the quotes.
However I feel there has to be a simpler solution for this out there. Any input would be appreciated. Thanks!
You could use indexOf and lastIndexOf to get the position of the quotes:
var openQuote = myString.indexOf('"'),
closeQuote = myString.lastIndexOf('"');
Then you can validate they are not the same position, and use substring to retrieve the code:
var code = myString.substring(openQuote, closeQuote + 1);
Regex:
var a = "I'm sorry the code \"codehere\" is not valid";
var m = a.match(/"[^"]*"/ig);
alert(m[0]);
Try this:
var str = "I'm sorry the code \"cod\"eh\"ere\" is not valid";
alert(str.replace(/^[^"]*"(.*)".*$/g, "$1"));
You could use Javascript's match function. It takes as parameter, a regular expression. Eg:
/\".*\"/
Use regular expressions! You can find a match using a simple regular expressions like /"(.+)"/ with the Javascript RegExp() object. Fore more info see w3schools.com.
Try this:
var msg = "I'm sorry the code \"codehere\" is not valid";
var matchedContent = msg.match(/\".*\"/ig);
//matchedContent is an array
alert(matchedContent[0]);
You should use a Regular Expression. This is a text pattern matcher that is built into the javascript language. Regular expressions look like this: /thing to match/flags* for example, /"(.*)"/, which matches everything between a set of quotes.
Beware, regular expressions are limited -- they can't match nested things, so if the value inside quotes contains quotes itself, you'll end up with a big ugly mess.
*: or new RegExp(...), but use the literal syntax; it's better.
You could always use the .split() string function:
var mystring = 'I\'m sorry the code "codehere" is not valid' ;
var tokens = [] ;
var strsplit = mystring.split('\"') ;
for(var i=0;i<strsplit.length;i++) {
if((i % 2)==0) continue; // Ignore strings outside the quotes
tokens.push(strsplit[i]) ; // Store strings inside quotes.
}
// Output:
// tokens[0] = 'codehere' ;

Regular expressions for parsing "real" words in JavaScript

I've got some text where some words are "real" words, and others are masks that will be replaced with some text and that are surrounded with, say, "%". Here's the example:
Hello dear %Name%! You're %Age% y.o.
What regular expression should I use to get "real" words, without using lookbehind, because they don't exist in JavaScript?
UPD: I want to get words "Hello", "dear", "you're", "y.o.".
If I've understood your question correctly this might work.
I would go about it the other way around, instead of finding the real words I would remove the "fake-words."
s = "Hello dear %Name%! You're %Age% y.o."
realWords = s.replace(/%.*?%/g, "").split(/ +/)
You could use split to get the words and filter the words afterwards:
var str = "Hello dear %Name%! You're %Age% y.o.", words;
words = str.split(/\s+/).filter(function(val) {
return !/%[^%]*%/.test(val);
});
To do a search and replace with regexes, use the string's replace() method:
myString.replace(/replaceme/g, "replacement")
Using the /g modifier makes sure that all occurrences of "replaceme" are replaced. The second parameter is an normal string with the replacement text.
You can match the %Something% matches using %[^%]*?%, but how are you storing all of the individual mask values like Name and Age?
Use regular expression in Javascript and split the string based on matching regular expression.
//javascript
var s = "Hello dear %Name%! You're %Age% y.o.";
words = s.split(/%[^%]*?%/i);
//To get all the words
for (var i = 0; i < words.length; i++) {
}

Categories

Resources