split hindi word maintaining conjuncts using javascript - javascript

i am trying out bellow code
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "कसौटी";
var res = str.split("");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
my result should be क, सौ, टी
however it turns out to be क,स,ौ,ट,ी
what exactly can be done to get the above result
is there a way to split hindi character while maintaing the conjuncts
any way in which we split utf -8 encoded characters then merge them back to get above result
please provide details in code , on how exactly it can be done

Related

Is this the best way to calculate a string [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was playing a bit more with html to add to my website, and following this question Dissappear content with html, I now have a phone number box and my javascript code tells me how many characters are in the inputs box, but also includes the spaces. Is there anyway for my javascript to calculate how many characters there are excluding the the spaces?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to return the number of characters in the string "Hello World!".</p>
<input id="id"></input>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var a = document.getElementById("id").value
var str = a
var n = str.length;
if(n==10) {
document.getElementById("demo").innerHTML = "Equal to 10"
}
else {
document.getElementById("demo").innerHTML = "Not equal to 10"
}
}
</script>
</body>
</html>
There's quite a bit that you can and should do to your code (both the HTML and the JavaScript). Some of it will make it more optimized and some of it has to do with the fact that you are using outdated techniques that should be replaced with the modern, standard approach.
See comments inline for details:
<!DOCTYPE html>
<html>
<!-- An HTML document must have a <head> section that
contains a non-empty <title> element. -->
<head>
<title>My Fun Page</title>
</head>
<body>
<p>Click the button to return the number of characters in the string "Hello World!".</p>
<input id="id"> <!-- input elements don't have a closing tag -->
<!-- Don't use HTML event attributes to bind JavaScript callbacks.
Do your event binding in JavaScript -->
<button>Try it</button>
<p id="demo"></p>
<script>
// This is the modern way to bind events:
document.querySelector("button").addEventListener("click", myFunction);
// Just get your element references once, not each time the function runs
const demo = document.getElementById("demo");
const input = document.getElementById("id");
function myFunction() {
// Variables are fine, but they don't help you when you
// are only going to use their value once. In that case
// just refer to what you need:
if(input.value.length == 10) {
// Don't use .innerHTML if you can help it as it has security
// and performance implications. Since you aren't working with
// any HTML anyway here, use .textContent
demo.textContent = "Equal to 10"
} else {
demo.textContent = "Not equal to 10"
}
}
</script>
</body>
</html>
No where in your code do you define "Hello World". Remove the "magic number" 10 (which should be 11, unless spaces and punctuation do not count) and replace it with the desired word length.
Also, use triple-equals (===) for value and type comparison.
const
targetWord = "Hello World",
targetLength = targetWord.length;
function myFunction() {
const str = document.getElementById("id").value;
document.getElementById("demo").textContent = str.length === targetLength
? `Equal to ${targetLength}`
: `Not equal to ${targetLength}`;
}
<p>Click the button to return the number of characters in the string "Hello World!".</p>
<input id="id" value="Hello World" />
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
This is how I would do it.
The ternary expression is maybe not easy to understand for beginners, but I like them.
<script>
function myFunction() {
const str = document.getElementById("id").value
const message = str.length === 10 ? "Equal to 10" : "Not equal to 10"
document.getElementById("demo").innerHTML = message
}
</script>

How to split string from particular word in javascript?

[
{
docPath: "c:\Project\Indofebweb\Attachment\images\indofab.png",
}
]
I want to split the string from \Attachment and get the rest of the string e.g
\Attachment\images\indofab.png . How can I do this?
For this particular string you can use :
var arr=test.split("Indofebweb");
var restString=arr[1]; //-----contains string \Attachment\images\indofab.png
You can make your own logic using split() function.
UPDATED CODE----TRY THIS----BELOW HTML
<!DOCTYPE html>
<html>
<body>
<p> a string display :</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "c:\\Project\\Indofebweb\\Attachment\\images\\indofab.png";;
var arr=str.split("Indofebweb");
document.getElementById("demo").innerHTML = arr[1];
}
</script>
</body>
</html>
use str.match()
path.match(/\\Attachment.*/);
this will return everything after and including "\Attachment". Keep in mind that backslashes need to be escaped.

How to replace "#[" anywhere in the string

My question is about regular expressions. I want to replace "#[" anywhere in the string. Tried with [#[] expression, but it's not working, because it replaces # anywhere in the string.
you can use the regex
/#\[/g
function replace(str){
return str.replace(/#\[/g, '');
}
console.log(replace('#[]'));
console.log(replace('abcd#[absd]ahah#[ahah'));
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">Visit Microsoft! #[</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/#\[/g, "W3Schools");
document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>

Simple Javascript on html textbox conversion. Doesn't work twice

I'm using the replace method and if I type in "test test" only the first test gets converted to good so it'll become "good test". I'm at a loss on why this is happening. On a side question, if I was to add 20 other words that I would like to replace, would I have to create 20 different str.replace?
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>
<script>
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace("test", "good");
document.getElementById("secondbox").value = res;
}
</script>
</body>
</html>
Any help would be greatly appreciated
Use regex, change "good" to /good/g
function myFunction() {
var str = document.getElementById("firstbox").value.toLowerCase()
var res = str.replace(/test/g, "good");
document.getElementById("secondbox").value = res;
}
<p>Click the button to replace "Test" with "Good"</p>
<textarea id="firstbox"></textarea>
<textarea id="secondbox"></textarea>
<button onclick="myFunction()">Change</button>

Javascript - multiplying a value with itself

I want to write a script that multiplies any number in a text field with itself by the push of a button and gives the result as an alert.
I'm completely new to Javascript (and have to write my first exam later today).
The syntax is killing me, sometimes so similar to Java, but than again not.
Here's what I came up with so far:
<!DOCTYPE html>
<html>
<head>
<script>
function myMultiply()
{
var x= $('#num1').val();
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
</script>
</head>
<body>
<input type="text" id="num1">
<button onclick="myMultiply()">Try it</button>
<p>By clicking the button above, the value in the text field will be multiplied with itself.</p>
</body>
</html>
You'll want to make sure you parse the input value as it will be a string when you query for it. To operate on it using multiplication, you need a number. You'll usually want to pass 10 as the second radix parameter as there are different implementations of parseInt
function myMultiply() {
var x = parseInt($('#num1').val(), 10);
var y = x*x;
alert(x + " times " + x + " equals " + y);
return false;
}
You cant multiply string it will be concatenated, parse value to int using parseInt first
parseInt
function myMultiply()
{
var x= parseInt($('#num1').val(), 10);
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
try replacing var y=x*x; with var y=Number(x)*Number(x);
Along with other answers indicating you should parseInt it should be noted that you aren't currently including jQuery (which gives you access to the $(".element") notation).
jQuery is a very common javascript library that saves a lot of time for very common Javascript tasks (selectors, events etc). You'll see the $() notation in many tutorials and to use it you need to include jQuery.
This will work:
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function myMultiply()
{
var x= parseInt( $('#num1').val(), 10 );
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
</script>
</head>
<body>
<input type="text" id="num1" />
<button onclick="myMultiply()">Try it</button>
<p>By clicking the button above, the value in the text field will be multiplied with itself.</p>
</body>
</html>
Your code is fine. You are simply missing the jquery include.
Add <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> right above your other script and everything works unchanged.
Javascript will parse strings and convert them to numbers automatically when it sees that you are trying to multiply. "4" * "2" is 8, not "44" or "42" or any other magical combination. You have a syntax error by referring to $ without actually including jQuery as a required script, so the function ends up being undefined.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function myMultiply()
{
var x= $('#num1').val();
var y= x*x;
alert(x+" times "+x+" equals "+y);
return false;
}
</script>
</head>
<body>
<input type="text" id="num1">
<button onclick="myMultiply()">Try it</button>
<p>By clicking the button above, the value in the text field will be multiplied with itself.</p>
</body>
</html>

Categories