Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert upper to lower and replace spaces with dashes?

I want to convert good bunch of url text.

from

CUSTOMER FAQS
HOW wE can HELP
PLANNING YOUR BUDGET
CUSTOMER CASE STUDIES
TENANT DISPUTES
EXIT STRATEGIES
USEFUL dOCUMENTS
USEFUL lINKS

to

customer-faqs
how-we-can-help
planning-your-budget
customer-case-studies
tenant-disputes
exit-strategies
useful-documents
useful-links

Is there any online or offline tool which can do this?

I want to do both thing at once.

like image 227
Jitendra Vyas Avatar asked Dec 22 '09 07:12

Jitendra Vyas


People also ask

How do you replace spaces with dashes?

Use the replace() method to replace spaces with dashes in a string, e.g. str. replace(/\s+/g, '-') . The replace method will return a new string, where each space is replaced by a dash.

How do you change spaces to dashes in Excel?

You can also click the Home tab in the Ribbon and select Replace in the Find & Select group. In the Find what box, type a space. In the Replace with box, type an underscore, dash, or other value. If you want to replace the space with nothing, leave the box blank.

How do I change a hyphen to a space in Python?

sub('\s+', '-', "How are you"); # To replace any sequence of 1 or more "space" by one hyphen: >>> re. sub(' +', '-', "How are you");

How do I remove a space and hyphen from a string?

var new_string = string. replace(/-|\s/g,""); a|b will match either a or b , so this matches both hyphens and whitespace.


2 Answers

value = value.toLowerCase().replace(/ /g,'-');
  • toLowerCase -> convert this string to all lower case
  • replace(/ /g,'-') -> Globally replace (/g) all spaces (/ /) with the string -

See also:

  • Convert JavaScript String to be all lower case?
  • Regex for replacing a single-quote with two single-quotes

If you just want to have this functionality and use it locally in your browser, you can make yourself a simple html page and save it to your desktop as convert.html (or whatever). However if you're going to go that far, I'd just use a shell script/command as one of the other answers posted.

<html>
<body>

    <h2>Input</h2>
    <textarea id="input"></textarea>
    <button onClick="doConvert()">Convert</button>

    <hr/>
    <h2>Output</h2>
    <textarea id="output"></textarea>

    <script type="text/javascript">
        function doConvert() {
            var value = document.getElementById('input').value;
            var newValue = value.toLowerCase().replace(/ /g,'-');
            document.getElementById('output').value = newValue;
        }
    </script>

</body>
</html>
like image 193
T. Stone Avatar answered Sep 21 '22 06:09

T. Stone


YOURTEXT.toLowerCase().replace(/ /g,"-")
like image 26
YOU Avatar answered Sep 19 '22 06:09

YOU